public class WaitDemo {
private static boolean flag = false;
private static Object object = new Object();
public static void main(String[] args) throws InterruptedException {
new Thread(() -> {
synchronized (object) {
if (!flag) {
try {
System.out.println("flag is false");
System.out.println(object+"进入等待状态");
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("flag is true");
}).start();
Thread.sleep(2000L);
new Thread(() -> {
synchronized (object) {
flag = true;
object.notify();
System.out.println(object+"被唤醒");
}
}).start();
}
}
(1)生产者消费者模型图
(2)编码实战
public class Broker {
//当前库存数
private static int num;
//规定最大库存数量
private static final int TOTAL = 20;
/**
* 生产者生产产品存入库存
*/
public synchronized void put(){
//先判断库存有没有满
if(num < TOTAL){
//库存没有满时,生产者生产
System.out.println("---库存新增一个,当前库存为:"+ ++num);
//唤醒消费者消费
notifyAll();
}else{
try {
//库存满时,生产这进入等待状态
System.out.println("***库存已满,生产者等待生产");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 消费者消费库存
*/
public synchronized void take(){
//先判断是否有库存
if(num>0){
System.out.println("---库存减少1个,当前库存为:"+ --num);
//唤醒生产者
notifyAll();
}else{
try {
System.out.println("***暂无库存,消费者等待消费");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Producer implements Runnable {
private Broker broker;
public Producer(Broker broker) {
this.broker = broker;
}
@Override
public void run() {
while (true) {
System.out.println("###生产者生产一件商品");
broker.put();
}
}
}
public class Consumer implements Runnable {
private Broker broker;
public Consumer(Broker broker) {
this.broker = broker;
}
@Override
public void run() {
while (true) {
System.out.println("###消费者消费一件商品");
broker.take();
}
}
}
public static void main(String[] args) {
//创建中间商
Broker broker = new Broker();
//生产者线程
for (int i = 0; i < 5; i++) {
new Thread(new Producer(broker)).start();
}
//消费者线程
for (int i = 0; i < 5; i++) {
new Thread(new Consumer(broker)).start();
}
}
(1)字节管道流
public class ByteStreamReader implements Runnable {
private PipedInputStream pipedInputStream;
public ByteStreamReader(PipedInputStream pipedInputStream) {
this.pipedInputStream = pipedInputStream;
}
@Override
public void run() {
try {
if(pipedInputStream != null){
//读取内存中中的数据
String str = new BufferedReader(new InputStreamReader(pipedInputStream)).lines().collect(Collectors.joining("\n"));
System.out.println("当前线程:"+Thread.currentThread().getName()+"读取内存中的数据:"+str);
}
pipedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
//创建管道输入流
PipedInputStream pipedInputStream = new PipedInputStream();
//创建管道输出流
PipedOutputStream pipedOutputStream = new PipedOutputStream();
//输入流与输出流建立连接
pipedOutputStream.connect(pipedInputStream);
//启动线程,将输入流作为参数传输进去
new Thread(new ByteStreamReader(pipedInputStream)).start();
//创建字符输入流
BufferedReader bufferedReader = null;
System.out.print("当前线程:"+Thread.currentThread().getName()+"向内存中写入数据:");
//将控制台输入的内容转化成流
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//写入内存
pipedOutputStream.write(bufferedReader.readLine().getBytes());
pipedOutputStream.close();
if(bufferedReader != null){
bufferedReader.close();
}
}
(1)join()方法简介
public final void join() throws InterruptedException;
public final synchronized void join(long millis) throws InterruptedException;
public final synchronized void join(long millis, int nanos) throws InterruptedException;
(2)join的使用
public class JoinDemo {
public static int num = 0;
public void add() {
num++;
}
public static void main(String[] args) {
JoinDemo joinDemo = new JoinDemo();
Thread thread = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ":开始执行");
System.out.println(Thread.currentThread().getName() + ":执行num+1");
joinDemo.add();
System.out.println(Thread.currentThread().getName() + ":结束执行");
}, "线程1");
new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + ":开始执行");
thread.start();
/**join方法控制让线程2中的线程1先执行完成以后在执行线程2后面的操作*/
thread.join();
if (num == 1) {
System.out.println(Thread.currentThread().getName() + ":拿到的num为:" + num);
}
System.out.println(Thread.currentThread().getName() + ":结束执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "线程2").start();
}
}
(1)Condition简介
(2)案例实战
public class ConditionDemo implements Runnable{
private static Lock lock = new ReentrantLock();
private static Condition condition = lock.newCondition();
@Override
public void run() {
try{
lock.lock();
condition.await();
System.out.println("Thread is going on");
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new ConditionDemo());
//启动线程
thread.start();
//睡眠2s
Thread.sleep(2000);
//加锁,因为condition在调用await()方法时,会释放锁资源,所以要重新加锁
lock.lock();
//唤醒
condition.signal();
//解锁
lock.unlock();
}
}
新建的线程thread调用start()方法后执行run()方法,此时掉用lock.lock()方法进行加锁,此时线程获得锁,继续执行condition.await()方法,这个时候线程会释放刚才获得的锁资源,将线程加入到condition维护的等待队列中,等调用condition.signal()方法后,会唤醒condition等待对类中的一个线程加入到AQS对列中去,直至唤醒的线程重新获取所资源后才能继续向下执行。
public class ConditionDemo {
private int queueSize=10;
//定义优先队列,大小初始化为10
private PriorityQueue<Integer> queue = new PriorityQueue<>(queueSize);
//定义ReentrantLock,Condition要配合锁使用
private Lock lock = new ReentrantLock();
//定义生产者的Condition对象
private Condition producer = lock.newCondition();
//定义消费者的Condition对象
private Condition consumer = lock.newCondition();
class Consumer extends Thread{
volatile boolean flag = true;
private void consume(){
//循环调用
while(flag){
//加锁
lock.lock();
try{
/**
* 如果队列是空就让消费者停止消费,进入等待状态,循环等待,
* 保证不会在有消费者线程去执行await()方法
*/
while(queue.isEmpty()){
try{
System.out.println("队列空,等待数据");
consumer.await();
} catch (InterruptedException e) {
//发生异常结束方法执行
flag=false;
}
}
//队列弹出一个元素
queue.poll();
//唤醒生产者
producer.signal();
System.out.println("从队列中取走一个元素,队列剩余"+queue.size()+"个元素");
}finally {
//最后一定要进行解锁操作
lock.unlock();
}
}
}
@Override
public void run() {
consume();
}
}
class Producer extends Thread{
volatile boolean flag = true;
private void produce(){
//循环调用
while(flag){
//加锁
lock.lock();
try{
/**
* 判断队列是否已满,如果队列的大小等于规定好的队列长度
* 就让生产者进行等待
*/
while(queue.size() == queueSize){
try {
System.out.println("队列满,等待有空余空间");
producer.await();
}catch (InterruptedException e){
//发生异常结束方法执行
flag=false;
}
}
//生产一个元素
queue.offer(1); //每次插入一个元素
//唤醒消费者
consumer.signal();
System.out.println("向队列中插入一个元素,队列剩余"+queue.size()+"个元素");
}finally {
lock.unlock();
}
}
}
@Override
public void run() {
produce();
}
}
public static void main(String[] args) {
ConditionDemo conditionDemo = new ConditionDemo();
Producer producer = conditionDemo.new Producer();
Consumer consumer = conditionDemo.new Consumer();
producer.start();
consumer.start();
producer.interrupt();
consumer.interrupt();
}
}