java wait notify notifyAll and 多线程顺序打印ABCD
来源:互联网 发布:淘宝特卖网 编辑:程序博客网 时间:2024/05/22 00:05
wait()?notify()?notifyAll()?
wait() : It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify(). The wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method. The wait() method is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism. In other words, it is not possible for us to implement the wait() method purely in Java: it is a native method.
General syntax for calling wait() method is like this:
synchronized( lockObject ){ while( ! condition ) { lockObject.wait(); } //take the action here;}
notify() : It wakes up one single thread that called wait() on the same object. It should be noted that calling notify() does not actually give up a lock on a resource. It tells a waiting thread that that thread can wake up. However, the lock is not actually given up until the notifier’s synchronized block has completed. So, if a notifier calls notify() on a resource but the notifier still needs to perform 10 seconds of actions on the resource within its synchronized block, the thread that had been waiting will need to wait at least another additional 10 seconds for the notifier to release the lock on the object, even though notify() had been called.
General syntax for calling notify() method is like this:
synchronized(lockObject) { //establish_the_condition; lockObject.notify(); //any additional code if needed}
notifyAll() : It wakes up all the threads that called wait() on the same object. The highest priority thread will run first in most of the situation, though not guaranteed. Other things are same as notify() method above.
General syntax for calling notify() method is like this:
synchronized(lockObject) { establish_the_condition; lockObject.notifyAll();}
生产者消费者模式
package com.demo;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * Created by leslie on 17/7/2. */public class ThreadDemo { //生产者 消费者问题 static class Producer implements Runnable{ private final List<Integer> taskQueue; private final int MAX_CAPACITY; public Producer(List<Integer> taskQueue, int MAX_CAPACITY) { this.taskQueue = taskQueue; this.MAX_CAPACITY = MAX_CAPACITY; } public void run() { int count = 0; while (true){ try { produce(count++); } catch (InterruptedException e) { e.printStackTrace(); } } } public void produce(int i) throws InterruptedException { synchronized (taskQueue){ while (taskQueue.size()==MAX_CAPACITY){ try { System.out.println("queue is full "+Thread.currentThread().getName()+" is waiting,queuesize is "+taskQueue.size()); taskQueue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Thread.sleep(1000); taskQueue.add(i); System.out.println(Thread.currentThread().getName()+" Produce added "+i); taskQueue.notifyAll(); } } } static class Consumer implements Runnable{ private final List<Integer> taskQUeue; public Consumer(List<Integer> taskQUeue) { this.taskQUeue = taskQUeue; } public void run() { while (true){ try { consumer(); } catch (InterruptedException e) { e.printStackTrace(); } } } private void consumer() throws InterruptedException { synchronized (taskQUeue){ while (taskQUeue.isEmpty()){ try { System.out.println("queue is full "+Thread.currentThread().getName()+" is waitting"); taskQUeue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Thread.sleep(1000); int i = taskQUeue.remove(0); System.out.println(Thread.currentThread().getName()+" Consumer "+i); taskQUeue.notifyAll(); } } } public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(5); List<Integer> taskQueue = new ArrayList<Integer>(); int MAX_CAPACITY = 5; service.execute(new Thread(new Producer(taskQueue, MAX_CAPACITY), "Producer")); service.execute(new Thread(new Consumer(taskQueue), "Consumer1")); service.execute(new Thread(new Consumer(taskQueue), "Consumer2")); service.shutdown(); }}
问题来了,对于顺序打印的问题该如何解决
package com.demo;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.atomic.AtomicInteger;/** * Created by leslie on 17/7/2. */public class SeqPrint { static class Worker extends Thread{ private AtomicInteger count; private String word; private int order; private int runCount; Worker(AtomicInteger count, String word, int order, int runCount) { this.count = count; this.word = word; this.order = order; this.runCount = runCount; } @Override public void run() { while(true){ synchronized (count){ if(count.get()%runCount==order){ System.out.println(word); count.getAndAdd(1); count.notifyAll(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }else{ try { count.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(4); AtomicInteger count = new AtomicInteger(0); int runCOunt = 4; service.execute(new Worker(count,"A",0,runCOunt)); service.execute(new Worker(count,"B",1,runCOunt)); service.execute(new Worker(count,"C",2,runCOunt)); service.execute(new Worker(count,"D",3,runCOunt)); service.shutdown(); }}
- java wait notify notifyAll and 多线程顺序打印ABCD
- java多线程wait() ,notify() notifyAll()
- Java多线程中wait, notify and notifyAll的使用
- 【Java多线程】之五:wait, notify and notifyAll
- Java多线程中wait, notify and notifyAll的使用
- Java多线程中wait, notify and notifyAll的使用
- wait(), notify() and notifyAll()
- java多线程之 wait(),notify(),notifyAll()
- java多线程之 wait(),notify(),notifyAll() 整理
- Java多线程之wait(),notify(),notifyAll()
- java多线程之 wait(),notify(),notifyAll()[迁]
- Java多线程之wait(),notify(),notifyAll()
- Java多线程,wait()、notify()、notifyAll()详解
- Java多线程之wait(),notify(),notifyAll() .
- Java多线程之synchronized,wait(),notify(),notifyAll()
- Java多线程之wait(),notify(),notifyAll()
- Java多线程之wait(),notify(),notifyAll()
- Java多线程3:wait、notify和notifyAll
- 彻底弄懂最短路径问题
- Linux菜鸟笔记——文件与目录的压缩与打包
- sublimeText的列编辑
- linux系统日常管理
- hex文件格式
- java wait notify notifyAll and 多线程顺序打印ABCD
- HDU 4775 Infinite Go 并查集
- 【JAVA集合详解】HashMap
- 【信源编码 作业三】基于LZO算法的词典压缩器实现
- Window server 2008 搭建FTP服务器
- 自启动程序慢
- AWE & PAE
- frida初体验
- Linux:shell脚本命令: >/dev/null 2>&1 的理解