wait和notify简单应用1
来源:互联网 发布:mac ae插件安装位置 编辑:程序博客网 时间:2024/06/06 05:21
import java.util.ArrayList;import java.util.List;import java.util.Random;//一个线程去读取List的长度,如果List为空的时候,线程就wait,如果List中有值了,就notify去获取它的长度public class ListAwit {private static List<Integer> list = new ArrayList<Integer>();final static Random rand = new Random();public static void main(String[] args) {final ListAwit la = new ListAwit();new Thread(new Runnable(){public void run() {while(true){if(list.size() > 0){synchronized(la){la.notify();}}sleep();}}}).start();while(true){try {System.out.println(Thread.currentThread().getName()+"等待.........");synchronized(la){la.wait();}sleep();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"##长度:"+list.size());System.out.println(list);}}private static void sleep(){try {int randint = rand.nextInt(2000);Thread.sleep(randint);list.add(randint);if(rand.nextInt(10)==5){list.clear();}} catch (InterruptedException e) {e.printStackTrace();}}/** * 运行结果: main等待.........main##长度:4[289, 887, 41, 1192]main等待.........main##长度:8[289, 887, 41, 1192, 1322, 238, 641, 1608]main等待......... */}
实现一种类似信号控制的程序
public class SynchronizedMutex {private Thread curOwner = null;public synchronized void acquire() throws InterruptedException{System.out.println("当前线程:"+Thread.currentThread().getName());while(curOwner != null){this.wait();}this.curOwner = Thread.currentThread();}public synchronized void release() throws InterruptedException{System.out.println("当前线程:"+Thread.currentThread().getName());while(curOwner == Thread.currentThread()){this.curOwner = null;this.notify();}}}
import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;import java.util.concurrent.TimeUnit;public class MutexTest{//private Semaphore mutex = new Semaphore(1);private SynchronizedMutex mutex = new SynchronizedMutex();public static void main(String[] args) {final MutexTest test = new MutexTest();Runnable task = new Runnable(){@Overridepublic void run() {while(true){int i = new Random().nextInt(3);switch(i){case 0:test.add();case 1:test.add1();case 2:test.del();default:test.add();}}}};ExecutorService exec = Executors.newCachedThreadPool();for(int i=0; i<5; i++){exec.execute(task);}}private static int value;public void add(){try {mutex.acquire();TimeUnit.SECONDS.sleep(1);System.out.println(Thread.currentThread().getName()+"增一:"+ (++value));mutex.release();} catch (InterruptedException e) {e.printStackTrace();}}public void add1(){try {mutex.acquire();TimeUnit.SECONDS.sleep(1);value = value + 2;System.out.println(Thread.currentThread().getName()+"增二:"+ value);mutex.release();} catch (InterruptedException e) {e.printStackTrace();}}public void del(){try {mutex.acquire();TimeUnit.SECONDS.sleep(1);System.out.println(Thread.currentThread().getName()+"减一:"+ (--value));mutex.release();} catch (InterruptedException e) {e.printStackTrace();}}/** * 运行结果: * 当前线程:pool-1-thread-2当前线程:pool-1-thread-4当前线程:pool-1-thread-5当前线程:pool-1-thread-3当前线程:pool-1-thread-1pool-1-thread-2减一:-1当前线程:pool-1-thread-2当前线程:pool-1-thread-2pool-1-thread-4增一:0当前线程:pool-1-thread-4当前线程:pool-1-thread-4pool-1-thread-4增二:2当前线程:pool-1-thread-4当前线程:pool-1-thread-4pool-1-thread-4减一:1当前线程:pool-1-thread-4当前线程:pool-1-thread-4pool-1-thread-4增一:2当前线程:pool-1-thread-4当前线程:pool-1-thread-4pool-1-thread-4减一:1当前线程:pool-1-thread-4当前线程:pool-1-thread-4 */}
- wait和notify简单应用1
- notify和wait在消息队列中的简单应用
- 一个简单的wait和notify示例
- wait和notify的简单例子
- java wait和notify 简单理解
- synchronized和wait()/notify()
- wait和notify实例
- wait()和notify()
- wait和notify使用
- notify 和wait
- notify 和wait 2
- wait(),notify()和锁
- wait和notify
- 理解wait和notify
- synchronized和wait()/notify()
- wait和notify实例
- 正确理解wait 和 notify
- wait()和notify()/notifyAll()
- 求一个数组中的最大和次大值
- Linux访问Windows共享文件夹,关于mount cifs的一个问题
- 匿名方法与委托关联调用实例
- 精通CSS.DIV网页样式与布局(二)
- 关于 树(tree)结构
- wait和notify简单应用1
- 软件销售的8个策略
- 子窗口调用父窗口变量和函数示例
- C语言辅助调试工具PRINT_INT(),便捷输出变量的地址
- 修改TabHost默认样式
- Collections.sort方法对list排序的两种方式
- javascript 优化总结(积累中)
- Oracle中视图的创建和处理方法
- android常见异常总结