多线程之经典生产者消费者问题
2014-10-27 09:38
375 查看
通过信号量,wait,nofity,synchronized来解决生产者消费者问题。
实例如下:
package thread;
public class ThreadWaitAndNotifyTest {
/**
* @param args
*/
public static void main(String[] args) {
Iphone iphone = new Iphone();
Thread t1 = new Thread(new Producer(iphone));
Thread t2 = new Thread(new Salesman(iphone));
t1.start();
t2.start();
}
}
class Iphone {
// Iphone是否已卖掉
private boolean isSaled = true;
private String name;
public synchronized void setName(int i) {
System.out.println(Thread.currentThread().getName()+"正在生产第"+(i)+"部IPhone...");
if (!isSaled) {
System.out.println(Thread.currentThread().getName()+"第" + (i-1)+ "部IPhone等待被卖掉。");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println(Thread.currentThread().getName()+"第" + (i-1)+ "部IPhone已被卖掉。");
}
this.name = "Iphone 5S("+i+")";
this.isSaled = false;
System.out.println(Thread.currentThread().getName()+"已生产第"+(i)+"部IPhone...");
notify(); // 通知消费者线程已生产IPhone,等待被卖掉。
}
public synchronized String getName(int i) {
if (isSaled) {
System.out.println(Thread.currentThread().getName()+"wait for sale start...");
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"wait for sale end...");
}
this.isSaled = true;
System.out.println(Thread.currentThread().getName() + "第"+i+"部IPhone已被卖掉。");
notify(); // 通知生产者线程该IPhone已被卖掉,可以生产下一部了。
return this.name;
}
}
class Producer implements Runnable {
Iphone iphone ;
public Producer(Iphone iphone) {
this.iphone = iphone;
}
@Override
public void run() {
for (int i=0;i<10;i++) {
try {
this.iphone.setName(i+1);
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Salesman implements Runnable {
Iphone iphone;
Salesman(Iphone iphone) {
this.iphone = iphone;
}
@Override
public void run() {
for (int i=0;i<10;i++) {
this.iphone.getName(i+1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
实例如下:
package thread;
public class ThreadWaitAndNotifyTest {
/**
* @param args
*/
public static void main(String[] args) {
Iphone iphone = new Iphone();
Thread t1 = new Thread(new Producer(iphone));
Thread t2 = new Thread(new Salesman(iphone));
t1.start();
t2.start();
}
}
class Iphone {
// Iphone是否已卖掉
private boolean isSaled = true;
private String name;
public synchronized void setName(int i) {
System.out.println(Thread.currentThread().getName()+"正在生产第"+(i)+"部IPhone...");
if (!isSaled) {
System.out.println(Thread.currentThread().getName()+"第" + (i-1)+ "部IPhone等待被卖掉。");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println(Thread.currentThread().getName()+"第" + (i-1)+ "部IPhone已被卖掉。");
}
this.name = "Iphone 5S("+i+")";
this.isSaled = false;
System.out.println(Thread.currentThread().getName()+"已生产第"+(i)+"部IPhone...");
notify(); // 通知消费者线程已生产IPhone,等待被卖掉。
}
public synchronized String getName(int i) {
if (isSaled) {
System.out.println(Thread.currentThread().getName()+"wait for sale start...");
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"wait for sale end...");
}
this.isSaled = true;
System.out.println(Thread.currentThread().getName() + "第"+i+"部IPhone已被卖掉。");
notify(); // 通知生产者线程该IPhone已被卖掉,可以生产下一部了。
return this.name;
}
}
class Producer implements Runnable {
Iphone iphone ;
public Producer(Iphone iphone) {
this.iphone = iphone;
}
@Override
public void run() {
for (int i=0;i<10;i++) {
try {
this.iphone.setName(i+1);
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Salesman implements Runnable {
Iphone iphone;
Salesman(Iphone iphone) {
this.iphone = iphone;
}
@Override
public void run() {
for (int i=0;i<10;i++) {
this.iphone.getName(i+1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
<p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">程序运行结果如下:</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第1部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第1部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第1部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第2部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第2部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第3部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第2部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第2部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第3部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第4部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第3部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第3部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第4部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第5部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第4部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第4部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第5部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第6部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第5部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第5部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第6部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第7部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第6部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第6部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第7部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第8部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第7部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第7部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第8部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第9部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第8部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第8部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第9部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0正在生产第10部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0第9部IPhone等待被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第9部IPhone已被卖掉。</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-0已生产第10部IPhone...</p><p style="margin: 0px auto; padding-top: 0px; padding-bottom: 0px; list-style: none;">Thread-1第10部IPhone已被卖掉。</p>
相关文章推荐
- 多线程第八篇 生产者消费者经典问题
- 多线程八 生产者消费者经典问题
- java多线程之生产者消费者经典问题
- java多线程总结六:经典生产者消费者问题实现
- java多线程总结六:经典生产者消费者问题实现
- java多线程总结六:经典生产者消费者问题实现
- java多线程-生产者消费者经典问题 基于BlockingQueue
- 生产者消费者问题 这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者
- 生产者与消费者以及多个容器的多线程问题(经典 集合了老师,同学以及自己的想法)
- java多线程之生产者消费者经典问题
- 多线程经典案例——生产者/消费者问题的Java实现与详解
- java 多线程经典例子——生产者与消费者的问题
- java多线程之生产者消费者经典问题
- 生产者消费者问题 这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者
- 黑马程序员——多线程操作经典实例:生产者消费者问题
- java多线程之生产者消费者经典问题
- 多线程八 生产者消费者经典问题
- 关于多线程的经典问题——生产者消费者,不能实现循环工作。
- java多线程之生产者消费者经典问题
- java多线程经典的生产者消费者问题