java多线程(生产消费)
2015-07-28 21:40
417 查看
/* 生产者,消费者。单生产,单消费。 */ class Resource { private String name; private int count=1; private boolean flag = false; public synchronized void set(String name) { if (flag) try{wait();}catch(InterruptedException e){} this.name = name +count; count++; System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name); flag = true; notify(); } public synchronized void out() { if (!flag) try{wait();}catch(InterruptedException e){} System.out.println(Thread.currentThread().getName()+".........消费者.........."+this.name); flag = false; notify(); } } class Producer implements Runnable { private Resource r; Producer(Resource r) { this.r=r; } public void run() { while (true) { r.set("馒头"); } } } class Consumer implements Runnable { private Resource r; Consumer(Resource r) { this.r=r; } public void run() { while (true) { r.out(); } } } class ProducerConsumerDemo { public static void main(String[] args) { Resource r = new Resource(); Producer pro = new Producer(r); Consumer con = new Consumer(r); Thread th1 = new Thread(con); Thread th2 = new Thread(pro); th1.start(); th2.start(); } }
/*
生产者,消费者
多生产多消费问题。
其中继续使用单生产,单消费中的if判断语句的话,当有线程在wait后被notify后会直接执行生产任务(因为被wait后的线程在线程池中,notify后不会判断if)
所以此时就使用while判断。while解决了notify的线程不会往上执行判断语句。
但是如果当四个线程中三个都已经wait。最后一个线程执行notify(因为是随机的)被唤醒的是自己本方的线程,那么线程会在执行一次生产或者消费任务后继续wait。
这就造成死锁。解决办法就是每次都是唤醒notifyAll()。
*/
class Resource
{
private String name;
private int count=1;
private boolean flag = false;
public synchronized void set(String name)
{
while (flag)
try{wait();}catch(InterruptedException e){}
this.name = name +count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者...."+this.name);
flag = true;
notifyAll();
}
public synchronized void out()
{
while (!flag)
try{wait();}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+".........消费者.........."+this.name);
flag = false;
notifyAll();
}
}
class Producer implements Runnable
{
private Resource r;
Producer(Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.set("馒头");
}
}
}
class Consumer implements Runnable
{
private Resource r;
Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.out();
}
}
}
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread th0 = new Thread(pro);
Thread th1 = new Thread(con);
Thread th2 = new Thread(pro);
Thread th3 = new Thread(con);
th0.start();
th1.start();
th2.start();
th3.start();
}
}
相关文章推荐
- Spring Framework简介
- java 泛型详解(普通泛型、 通配符、 泛型接口,泛型数组,泛型方法,泛型嵌套)
- java 泛型详解(普通泛型、 通配符、 泛型接口,泛型数组,泛型方法,泛型嵌套)
- LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解
- Struts2中result配置中常见的几种视图转发类型
- Spring的Bean的生命周期
- 《Java程序性能优化》学习笔记之ArrayList和LinkedList
- 【Java】(1)Base64加密技术
- struts2的验证
- 3. Java面向对象之泛型-指定多个泛型
- 3. Java面向对象之泛型-指定多个泛型
- jdk配置
- Java替换字符或十进制数的字符串
- Eclipse 一直提示 loading descriptor for 的解决方法
- Spring结合Hessian协议远程调用方法名不能重载
- spring 和 mybatis 的整合
- 记录JAVA学习的第一天
- JAVA数据类型
- 2. Java面向对象之泛型-构造方法中使用
- 2. Java面向对象之泛型-构造方法中使用