生产和消费间关系问题
要求:
1、必须先生产才能消费
2、线程控制
3、打印输出
注意:
1、本案例主要借助于同步,wait,notifyAll 来实现进程间通信。
2、采用全局变量 flag 的方式在不同线程间传递,来控制流程可借鉴。
3、在使用wait的时候一定要和notifyAll一起用,针对共享对象操作的类一定要加同步。
思路:
queue 类 存储 n 0
生产者类 线程 每次对 n 增加
消费者类 线程 获取n的值
线程间通信的方法:
wait():中断方法的执行,使线程等待
notify():唤醒处于等待的某个线程,使其结束等待
notifyAll():唤醒所有等待的线程,使他们结束等待
代码如下:
queue类:【生产者生产和消费者消费均从这个地方放置或取出】
package com.imooc.queue;
public class Queue {
private int n;
boolean flag = false;
//模仿消费
public synchronized int getN() {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费: " + n);
flag = false;//消费完毕,容器中没有数据
notifyAll();
return n;
}
//模仿生产
public synchronized void setN(int n) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产: " + n);
this.n = n;
flag = true;//生产完毕,容器中已有数据
notifyAll();
}
}
producer类:
package com.imooc.queue;
public class Producer implements Runnable{
Queue queue;
public Producer(Queue queue) {
this.queue = queue;
}
@Override
public void run() {
int i=0;
while (true){
queue.setN(i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
customer 类:
package com.imooc.queue;
public class Consumer implements Runnable {
Queue queue;
public Consumer(Queue queue) {
this.queue = queue;
}
@Override
public void run() {
while (true){
queue.getN();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主类:
package com.imooc.queue;
public class Test {
public static void main(String[] args) {
Queue queue = new Queue();
new Thread(new Producer(queue)).start();
new Thread(new Consumer(queue)).start();
}
结果为:
生产: 0
消费: 0
生产: 1
消费: 1
生产: 2
消费: 2
。。。。。
要求:
1、必须先生产才能消费
2、线程控制
3、打印输出
注意:
1、本案例主要借助于同步,wait,notifyAll 来实现进程间通信。
2、采用全局变量 flag 的方式在不同线程间传递,来控制流程可借鉴。
3、在使用wait的时候一定要和notifyAll一起用,针对共享对象操作的类一定要加同步。
思路:
queue 类 存储 n 0
生产者类 线程 每次对 n 增加
消费者类 线程 获取n的值
线程间通信的方法:
wait():中断方法的执行,使线程等待
notify():唤醒处于等待的某个线程,使其结束等待
notifyAll():唤醒所有等待的线程,使他们结束等待
代码如下:
queue类:【生产者生产和消费者消费均从这个地方放置或取出】
package com.imooc.queue;
public class Queue {
private int n;
boolean flag = false;
//模仿消费
public synchronized int getN() {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费: " + n);
flag = false;//消费完毕,容器中没有数据
notifyAll();
return n;
}
//模仿生产
public synchronized void setN(int n) {
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产: " + n);
this.n = n;
flag = true;//生产完毕,容器中已有数据
notifyAll();
}
}
producer类:
package com.imooc.queue;
public class Producer implements Runnable{
Queue queue;
public Producer(Queue queue) {
this.queue = queue;
}
@Override
public void run() {
int i=0;
while (true){
queue.setN(i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
customer 类:
package com.imooc.queue;
public class Consumer implements Runnable {
Queue queue;
public Consumer(Queue queue) {
this.queue = queue;
}
@Override
public void run() {
while (true){
queue.getN();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主类:
package com.imooc.queue;
public class Test {
public static void main(String[] args) {
Queue queue = new Queue();
new Thread(new Producer(queue)).start();
new Thread(new Consumer(queue)).start();
}
}
感谢慕课网
============结果为:
生产: 0
消费: 0
生产: 1
消费: 1
生产: 2
消费: 2
。。。。。