第一种方式 使用BlockingQueue 阻塞队列

public class Threads {

    public static void main(String[] args) {
final ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<>(10);
produce produce=new produce(queue);
consumer consumer=new consumer(queue);
consumer.start();
produce.start(); } static class produce extends Thread{
final ArrayBlockingQueue<Integer> integerArrayBlockingQueue; public produce(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
this.integerArrayBlockingQueue = integerArrayBlockingQueue;
} @Override
public void run() {
while (true){
Integer random=new Random().nextInt(10);
integerArrayBlockingQueue.add(random);
System.out.println("shen chan shu ju"+random);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} static class consumer extends Thread{
final ArrayBlockingQueue<Integer> integerArrayBlockingQueue; public consumer(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
this.integerArrayBlockingQueue = integerArrayBlockingQueue;
} @Override public void run() {
while (true){
try {
Integer element=integerArrayBlockingQueue.take();
System.out.println("xiao fei shu ju "+element);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} }
第二种方法利用 wait()和 notifyAll()
public class Threads {
private static String lock = "lock"; public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(10);
final Integer max = 10;
produce produce1 = new produce(list, max);
produce produce2 = new produce(list, max);
consumer consumer = new consumer(list, max);
consumer.start();
produce1.start();
produce2.start();
} static class produce extends Thread {
final List<Integer> list;
final Integer max; public produce(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
} @Override
public void run() {
while (true) {
synchronized (lock) {
while (list.size() > max) {
try {
lock.wait();
System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = new Random().nextInt(10);
list.add(random);
lock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
}
}
}
} static class consumer extends Thread {
final List<Integer> list;
final Integer max; public consumer(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
} @Override public void run() {
while (true) {
synchronized (lock) {
while (list.isEmpty()) {
try {
lock.wait();
System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = list.remove(0);
lock.notifyAll();
System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
} }
}
} }
第三种方法 ReentrantLock await() 和 signal() 实现
public class Threads {

    private Lock lock=new ReentrantLock();
final Condition notfull=lock.newCondition();
final Condition notempty=lock.newCondition();
public static void main(String[] args) {
final List<Integer> list = new ArrayList<>(10);
final Integer max = 10;
Threads threads = new Threads();
produce produce1 = threads.new produce(list, max);
produce produce2 = threads.new produce(list, max);
consumer consumer = threads.new consumer(list, max);
consumer.start();
produce1.start();
produce2.start();
} class produce extends Thread {
final List<Integer> list;
final Integer max; public produce(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
} @Override
public void run() {
while (true) {
lock.lock();
while (list.size() > max) {
try {
notfull.await();
System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = new Random().nextInt(10);
list.add(random);
notempty.signal();
System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
}
}
} class consumer extends Thread {
final List<Integer> list;
final Integer max; public consumer(List<Integer> list, Integer max) {
this.list = list;
this.max = max;
} @Override public void run() { while (true) {
lock.lock();
while (list.isEmpty()) {
try {
notempty.await();
System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int random = list.remove(0);
notfull.signal();
System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
} }
} }
												

最新文章

  1. python setup.py 管理
  2. 防止双击选中html中文字
  3. hdu3001 Travelling
  4. Java SAX Parser
  5. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
  6. Android中的常见时区
  7. Java线程状态:BLOCKED与WAITING的区别
  8. 扑克k,你知道的人物吗?
  9. Cpp again
  10. android做设计的每一个屏幕尺寸和分辨率(一个)
  11. Mac下通过brew安装指定版本的nodejs
  12. Gitlab源码库里代码提交后,如何触发jenkins自动构建?
  13. BeautifulSoup爬网页图片
  14. Beans 自动装配
  15. SVG之颜色、渐变和笔刷的使用
  16. POJ 2603
  17. 开源项目DataTimePicker实现时间和日期的选择
  18. html_常用技巧总结
  19. [洛谷P3829][SHOI2012]信用卡凸包
  20. WIN32 SDK对COM的支持

热门文章

  1. jmeter 安装
  2. 015模块&mdash;&mdash;起别名
  3. Oracle 12c 静默安装(脚本自动化)
  4. DBCP 连接池
  5. 一、docker的原理
  6. netcore项目在Windows部署:使用NSSM部署Windows服务
  7. 2019全国大学生信息安全竞赛部分Web writeup
  8. iframe内document事件监听
  9. Koa与Node.js开发实战(2)——使用Koa中间件获取响应时间(视频演示)
  10. This is very likely to create a memory leak 异常