要求:用两个线程模拟存票、售票过程。但要求每存入一张票,就售出一张票,售出后,再存入,直到售完为止。

用到的知识点:线程等待、唤醒、可能的线程中断异常

下面的方式一和方式二采用的是唤醒所有等待的线程,即wait()和notify()方法

方式一:继承Thread

class Tickets //定义(资源)票类
{
protected int size;//总票数
int number=0; //票号
Boolean available=false;//表示当前是否有票可售
public Tickets(int size)
{
this.size = size;
}
public synchronized void store() //同步方法,实现存票功能
{
if(available) //如果线程还有票可售,则存票线程等待
try{wait();}
catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"存入第【"+(++number)+"】张票");
available = true;
notify(); //存票后,唤醒售票线程开始售票
} public synchronized void sale() //同步方法,实现售票功能
{
if(!available) //如果线程没有票可售,则售票线程等待
try{wait();}
catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"售出第【"+(number)+"】张票");
available = false;
notify(); //售票后,唤醒存票线程开始存票
}
} class Productor extends Thread //定义生产者(存票)线程类
{
private Tickets t;
public Productor(Tickets t)
{
this.t = t;
}
public void run()
{
while(true)
{ if(t.number<t.size)
t.store();
else
System.exit(0);
}
}
} class Costomer extends Thread //定义消费者(售票)线程类
{
private Tickets t;
public Costomer(Tickets t)
{
this.t = t;
}
public void run()
{
while(true)
{ if(t.number<=t.size)
t.sale();
else
System.exit(0);
}
}
}
class TicketStoreSale3
{
public static void main(String[] args)
{
Tickets t = new Tickets(10); Productor t1 = new Productor(t);
Costomer t2 = new Costomer(t); t1.start();
t2.start();
}
}

方式二:实现Runnable接口

class Tickets //定义(资源)票类
{
protected int size;//总票数
int number=0; //票号
Boolean available=false;//表示当前是否有票可售
public Tickets(int size)
{
this.size = size;
}
public synchronized void store() //同步方法,实现存票功能
{
if(available) //如果线程还有票可售,则存票线程等待
try{wait();}
catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"存入第【"+(++number)+"】张票");
available = true;
notify(); //存票后,唤醒售票线程开始售票
} public synchronized void sale() //同步方法,实现售票功能
{
if(!available) //如果线程没有票可售,则售票线程等待
try{wait();}
catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"售出第【"+(number)+"】张票");
available = false;
notify(); //售票后,唤醒存票线程开始存票
}
} class Productor implements Runnable //实现一个接口,生产者(存票)线程类
{
private Tickets t;
public Productor(Tickets t)
{
this.t = t;
}
public void run()
{
while(true)
{ if(t.number<t.size)
t.store();
else
System.exit(0);
}
}
} class Costomer implements Runnable //实现一个接口,消费者(售票)线程类
{
private Tickets t;
public Costomer(Tickets t)
{
this.t = t;
}
public void run()
{
while(true)
{ if(t.number<=t.size)
t.sale();
else
System.exit(0);
}
}
}
class TicketStoreSale
{
public static void main(String[] args)
{
Tickets t = new Tickets(10); Thread t1 = new Thread(new Productor(t));
Thread t2 = new Thread( new Costomer(t)); t1.start();
t2.start();
}
}

方式三:在JDK1.5中提供了多线程升级解决方案。
将同步Synchronized替换成显式的Lock操作。
将Object中的wait,notify,notifyAll,替换成了Conditon对象。
该对象可以对Lock锁,进行获取。
该示例中,实现了本方只唤醒对方操作。

import java.util.concurrent.locks.*;
class Tickets //定义(资源)票类
{
protected int size;//总票数
int number=0; //票号
Boolean available=false;//表示当前是否有票可售
public Tickets(int size)
{
this.size = size;
}
Lock lock = new ReentrantLock(); //创建锁
Condition notFull = lock.newCondition(); //创建未满状态
Condition notEmpty = lock.newCondition();//创建未空状态 public void store() throws InterruptedException //同步方法,实现存票功能
{
lock.lock();
try
{
if(available) //如果线程还有票可售,则存票线程等待
notEmpty.await();
System.out.println(Thread.currentThread().getName()+"存入第【"+(++number)+"】张票");
available = true;
notFull.signal();//存票后,唤醒售票线程开始售票
}
finally
{
lock.unlock(); //释放锁资源
}
} public void sale() throws InterruptedException //同步方法,实现售票功能
{
lock.lock();
try
{
if(!available) //如果线程没有票可售,则售票线程等待
notFull.await();
System.out.println(Thread.currentThread().getName()+"售出第【"+(number)+"】张票");
available = false;
notEmpty.signal(); //售票后,唤醒存票线程开始存票
}
finally
{
lock.unlock(); //释放锁资源
}
}
} class Productor implements Runnable //实现一个接口,生产者(存票)线程类
{
private Tickets t;
public Productor(Tickets t)
{
this.t = t;
}
public void run()
{
while(true)
{ if(t.number<t.size)
try
{
t.store();
}
catch (InterruptedException e)
{
e.printStackTrace();
} else
System.exit(0);
}
}
} class Costomer implements Runnable //实现一个接口,消费者(售票)线程类
{
private Tickets t;
public Costomer(Tickets t)
{
this.t = t;
}
public void run()
{
while(true)
{ if(t.number<=t.size)
try
{
t.sale();
}
catch (InterruptedException e)
{
e.printStackTrace();
} else
System.exit(0);
}
}
}
class TicketStoreSaleLock
{
public static void main(String[] args)
{
Tickets t = new Tickets(10); Thread t1 = new Thread(new Productor(t));
Thread t2 = new Thread( new Costomer(t)); t1.start();
t2.start();
}
}

Java:多线程之生产者与消费者的更多相关文章

  1. java多线程解决生产者消费者问题

    import java.util.ArrayList; import java.util.List; /** * Created by ccc on 16-4-27. */ public class ...

  2. JAVA之旅(十五)——多线程的生产者和消费者,停止线程,守护线程,线程的优先级,setPriority设置优先级,yield临时停止

    JAVA之旅(十五)--多线程的生产者和消费者,停止线程,守护线程,线程的优先级,setPriority设置优先级,yield临时停止 我们接着多线程讲 一.生产者和消费者 什么是生产者和消费者?我们 ...

  3. java多线程模拟生产者消费者问题,公司面试常常问的题。。。

    package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...

  4. Java中的生产者、消费者问题

    Java中的生产者.消费者问题描述: 生产者-消费者(producer-consumer)问题, 也称作有界缓冲区(bounded-buffer)问题, 两个进程共享一个公共的固定大小的缓冲区(仓库) ...

  5. Java多线程实现生产者消费者延伸问题

    在操作系统中有一类问题被称为生产者消费者问题:意为,有数个生产者生产产品,有数个消费者消费产品,他们共享一定数量的缓存. 这里用java多线程编程,实现生产者消费者问题的一种延伸,橘子苹果问题. 题目 ...

  6. Java多线程-----实现生产者消费者模式的几种方式

       1 生产者消费者模式概述 生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题.生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理 ...

  7. 【多线程】java多线程实现生产者消费者模式

    思考问题: 1.为什么用wait()+notify()实现生产者消费者模式? wait()方法可以暂停线程,并释放对象锁 notify()方法可以唤醒需要该对象锁的其他线程,并在执行完后续步骤,到了s ...

  8. Java多线程之生产者消费者问题&lt;一&gt;:使用synchronized keyword解决生产者消费者问题

    今天看了一片博文,讲Java多线程之线程的协作,当中作者用程序实例说明了生产者和消费者问题,但我及其它读者发现程序多跑几次还是会出现死锁,百度搜了下大都数的样例也都存在bug,经过细致研究发现当中的问 ...

  9. JAVA多线程之生产者 消费者模式 妈妈做面包案例

    创建四个类 1.面包类 锅里只可以放10个面包 ---装面包的容器2.厨房 kitchen 生产面包 和消费面包  最多生产100个面包3.生产者4消费者5.测试类 多线程经典案例 import ja ...

  10. Java多线程_生产者消费者模式2

    在我的上一条博客中,已经介绍到了多线程的经典案列——生产者消费者模式,但是在上篇中用的是传统的麻烦的非阻塞队列实现的.在这篇博客中我将介绍另一种方式就是:用阻塞队列完成生产者消费者模式,可以使用多种阻 ...

随机推荐

  1. java 判断两个数是否异号

    java 整型int占4个字节32位,两个数异或后移动31位判断结果,如果是1则异号,如果是0则同号 public class ShowEnviromentViarible { public stat ...

  2. Linux学习之路--启动VNC服务

    我的Linux是Fedora 13,安装方法如下: 1.打开终端,执行 # yum install -y tigervnc tigervnc-server 2.编辑/etc/sysconfi/vncs ...

  3. Careercup - Google面试题 - 4699414551592960

    2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...

  4. 【Roman To Integer】cpp

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  5. 【桌面程序搞界面再也不怕了】:迅雷BOLT入门(一)开篇 附程序和源码

    本来想多蛤一下前因后果,突然意兴阑珊不想多说啦,直接帖效果吧. 这个是用迅雷BOLT把原来写的一个IE拦截器的界面重写了一下.界面效果是直接从单位的大屏系统改过来的,其中文本框部分,还请设计大屏的小姑 ...

  6. Codeforces Round #130 (Div. 2) C - Police Station 最短路+dp

    题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 seconds ...

  7. 802.11 wireless 三

    802.11 wireless 3watts,milliwatts,and Decibels瓦特(功率单位)的定义是1焦耳/秒微波炉1000瓦特,手机100-200毫瓦 decibels(分贝:比较能 ...

  8. idea新建项目完整过程

    参看下面博客 http://www.cnblogs.com/cnjava/archive/2013/01/29/2881654.html 突然,感觉idea其实挺麻烦的: 一.junit test做起 ...

  9. ios containerViewController

    - (void)replaceViewController:(UIViewController *)existingViewController withViewController:(UIViewC ...

  10. CSS 加载新方式

    Chrome 浏览器有意改变<link rel="stylesheet">的加载方式,当其出现在<body>中时,这一变化将更加明显.笔者决定在本文中进行详 ...