Java学习之线程通信(多线程(synchronized))--生产者消费者
分析线程经典案例生产者消费者
/**
共享数据
*/
class Resource
{
private String name;
private int count=1;
private boolean flag=false; public synchronized void set(String name)
{
if(flag)
try{this.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{this.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 p=new Producer(r);
Consumer c=new Consumer(r);
//实例化线程,指定线程任务
Thread t0=new Thread(p);
Thread t1=new Thread(p);
Thread t2=new Thread(c);
Thread t3=new Thread(c);
//开启线程,执行线程任务中的run方法
t0.start();
t1.start();
t2.start();
t3.start();
}
}
运行结果:
结果分析:
那么怎么再判断flag呢?while
代码如下:
class Resource
{
private String name;
private int count=1;
private boolean flag=false; public synchronized void set(String name) // 有两个线程t0 t1
{
while(flag)
try{this.wait();}catch(InterruptedException e){}
this.name=name+count;
count++;
System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
flag = true;
notify();
} public synchronized void out() // 有两个线程t2 t3
{
while(!flag)
try{this.wait();}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName() + "==消费者==" + this.name);
flag = false;
notify();
}
}
结果出现死锁:
结果分析:
通过分析,那能不能每次唤醒只唤醒对方线程(如生产者线程只唤醒消费者线程,消费者线程只唤醒生产者线程),查看Object对象方法中没有,但是有一个notifyAll()方法,实在不行就把所有阻塞线程(相同同步锁的线程)都唤醒。
class Resource
{
private String name;
private int count=1;
private boolean flag=false; public synchronized void set(String name) // 有两个线程t0 t1
{
while(flag)
try{this.wait();}catch(InterruptedException e){}
this.name=name+count;
count++;
System.out.println(Thread.currentThread().getName() + "...生产者..." + this.name);
flag = true;
notifyAll();
} public synchronized void out() // 有两个线程t2 t3
{
while(!flag)
try{this.wait();}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName() + "==消费者==" + this.name);
flag = false;
notifyAll();
}
}
结果:
这样就可以了。
总结:
if判断只能判断一次,如果执行等待方法,下次唤醒的线程获取执行权后(唤醒后的线程会从上次等待位置,向下执行)就不能再判断
while判断解决唤醒的线程获取执行权后无法判断
notify:只能唤醒任意一个线程,有可能会唤醒本方线程,while+notify会导致死锁
notifyAll:解决了死锁问题
Java学习之线程通信(多线程(synchronized))--生产者消费者的更多相关文章
- Java学习之线程通信(多线程(Lock))--生产者消费者
SDK1.5版本以后对synchronized的升级,其思想是:将同步和锁封装成对象,其对象中包含操作锁的动作. 代码: //1.导入包 import java.util.concurrent.loc ...
- 【多线程】java多线程实现生产者消费者模式
思考问题: 1.为什么用wait()+notify()实现生产者消费者模式? wait()方法可以暂停线程,并释放对象锁 notify()方法可以唤醒需要该对象锁的其他线程,并在执行完后续步骤,到了s ...
- Java并发包——线程通信
Java并发包——线程通信 摘要:本文主要学习了Java并发包里有关线程通信的一些知识. 部分内容来自以下博客: https://www.cnblogs.com/skywang12345/p/3496 ...
- java多线程模拟生产者消费者问题,公司面试常常问的题。。。
package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...
- Java多线程_生产者消费者模式2
在我的上一条博客中,已经介绍到了多线程的经典案列——生产者消费者模式,但是在上篇中用的是传统的麻烦的非阻塞队列实现的.在这篇博客中我将介绍另一种方式就是:用阻塞队列完成生产者消费者模式,可以使用多种阻 ...
- java多线程解决生产者消费者问题
import java.util.ArrayList; import java.util.List; /** * Created by ccc on 16-4-27. */ public class ...
- Java并发基础--线程通信
java中实现线程通信的四种方式 1.synchronized同步 多个线程之间可以借助synchronized关键字来进行间接通信,本质上是通过共享对象进行通信.如下: public class S ...
- Android-Java多线程通讯(生产者 消费者)&10条线程对-等待唤醒/机制的管理
上一篇博客 Android-Java多线程通讯(生产者 消费者)&等待唤醒机制 是两条线程(Thread-0 / Thread-1) 在被CPU随机切换执行: 而今天这篇博客是,在上一篇博客A ...
- 【多线程】--生产者消费者模式--Lock版本
在JDK1.5发布后,提供了Synchronized的更优解决方案:Lock 和 Condition 我们使用这些新知识,来改进例子:[多线程]--生产者消费者模式--Synchronized版本 改 ...
随机推荐
- MySQL-第四篇索引
1.创建索引的作用 创建索引的唯一作用就是加速对表的查询.索引通过使用快速路径访问方法来快速定位数据,从而减少了磁盘的I/O. 2.索引和表一样也是数据库中的一种对象,但它必须从属于某张表,不能独立存 ...
- tmux多终端工具
在Linux服务器上没有办法像在桌面系统一样开多个终端,所以有时后进行一些操作不是太方便,所以可以使用tmux工具,创建多个终端. 这里仅仅是简单的介绍一下如何创建多个终端和进行多个终端之间切换,tm ...
- Leetcode Lect1 String相关题目
Java 的 String 类基本用法介绍:http://www.runoob.com/java/java-string.html Java 的 String.substring 函数:https:/ ...
- 使用git、git-flow与gitlab工作
使用git.git-flow与gitlab工作 1. 摘要 在工作中使用git代替svn也有一段时间了,对于git的一些特性喜爱的同时也一直遇到相同的问题:“这时候应该打什么命令?”.相对于svn或者 ...
- axios 如何获取下载文件的进度条
exportFun(){ let _that = this const instance = this.axios.create({ onDownl ...
- ReactNative出现错误问题'React/RCTAssert.h' file not found
今天搭建一个rn的项目,项目可以运行但就是报一个错误, 查阅是由于Pods里的React结构改变了,配置没有改过来,所以出现找不到文件的问题 修改search paths 中 header searc ...
- Java并发(基础知识)—— Java中断机制
上文讲解了Java线程的创建.启动以及停止,在讲到停止线程时说到了Java中断,Java中断是停止线程的一种协作机制,本文打算对Java中断机制进行详细讲解. 在网上搜索Java中断机制,发现两篇好文 ...
- java 继承访问成员变量
package java09; //创建父类 public class Fu { int numFu = 10; int num =100; public void methodFu(){ Syste ...
- 【串线篇】数据库设计之加谈n-n
// n-n即(1-n)+(n-1): // 1-n:n-1:n-n:外键应该放在哪个表? //结论: //一对n:外键一定放在n的一端:别让一个人去记14亿,而让大家只记一个习大大 //n-n:”中 ...
- bzoj4987 Tree 树上背包
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4987 题解 一道还不错的题咯. 很容易发现一个结论:这 \(k\) 个点构成的一定是一个连通块 ...