java学习9:生产和消费问题,先生产才能消费,线程间通信与等待
来源:互联网 发布:java枚举单例模式 编辑:程序博客网 时间:2024/05/01 12:23
生产和消费间关系问题
要求:
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
。。。。。
阅读全文
0 0
- java学习9:生产和消费问题,先生产才能消费,线程间通信与等待
- 线程间通信--生产消费
- 线程 --生产和消费问题
- 线程间通信:生产线程和消费线程
- 线程---生产消费问题
- 【Java】----线程同步:生产-消费问题
- Java 线程同步 生产消费问题
- Java 多线程间通信 多生产 多消费问题
- java线程之生产与消费
- java线程之生产与消费
- java生产消费问题
- 多线程__【线程间通信】【等待唤醒机制】【多生产多消费】【Lock&Condition接口】
- 多线程__【线程间通信】【等待唤醒机制】【多生产多消费】【Lock&Condition接口】
- 多线程经典问题:馒头问题 生产和消费问题(线程的通信问题)
- java线程同步----生产消费
- Java模拟生产消费问题
- Java多线程的单生产单消费和多生产多消费问题的解决
- 经典的生产与消费线程同步问题
- nuc972项目随记
- pandas学习笔记
- 交换两个变量的值
- VC++/MFC中调用CHM帮助文档的方法--ShellExecute
- createQuery()和createNativeQuery()区别
- java学习9:生产和消费问题,先生产才能消费,线程间通信与等待
- Linux下更改目录及其下的子目录和文件的访问权限
- Druid 介绍及配置
- 热敏打印机
- 普元EOS中NUI的提示为英文,如何修改为中文提示
- 高级算法日记7:专题
- 欢迎使用CSDN-markdown编辑器
- jstl的jar包
- 【Hystrix系列】三、适用的熔断降级场景