• 并发编程基础之wait以及notify的用法


    一:概念

    线程通信中经常用到wait和notify,顾名思义,wait即让当前线程处于等待状态,notify通知锁对象

    上的另一个线程被唤醒,这里的唤醒是指可以去争夺锁资源,nofityAll是唤醒该对象上面所有处于

    wait状态的线程

    二:示例

    线程t2一运行就处于wait等待状态,然后线程t1运行notify,唤醒线程t2

    /**
     * 
     */
    package com.day2;
    
    /**
     * @author Administrator
     *
     */
    public class NotifyWaitThreadDemo {
    
    	private int count;
    
    	public static void main(String[] args) {
    
    		NotifyWaitThreadDemo demo = new NotifyWaitThreadDemo();
    
    		Thread t1 = new Thread("t1") {
    			public void run() {
    				synchronized (demo) {
    					for (int i = 0; i < 100; i++) {
    						demo.count = i;
    						if (demo.count == 50) {
    							System.out.println(Thread.currentThread().getName()+"发出通知");
    							demo.notify();
    						}
    					}
    				}
    
    			}
    		};
    
    		Thread t2 = new Thread("t2") {
    			public void run() {
    				while (true) {
    					synchronized (demo) {
    						System.out.println(Thread.currentThread().getName() + "开始等待");
    						try {
    							demo.wait();
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
    					}
    					System.out.println(Thread.currentThread().getName() + "跳出等待");
    					System.out.println("demo.count" + demo.count);
    					break;
    				}
    
    			}
    		};
    		
    		t2.start();
    		try {
    			Thread.sleep(100);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		t1.start();
    		
    	}
    }
    

      

    如果还有一个线程t3也处于wait状态,那么t1线程如果想唤醒t1和t3,就需要使用notifyAll

    /**
     * 
     */
    package com.day2;
    
    /**
     * @author Administrator
     *
     */
    public class NotifyWaitThreadDemo {
    
    	private int count;
    
    	public static void main(String[] args) {
    
    		NotifyWaitThreadDemo demo = new NotifyWaitThreadDemo();
    
    		Thread t1 = new Thread("t1") {
    			public void run() {
    				synchronized (demo) {
    					for (int i = 0; i < 100; i++) {
    						demo.count = i;
    						if (demo.count == 50) {
    							System.out.println(Thread.currentThread().getName()+"发出通知");
    							//demo.notify();
    							demo.notifyAll();
    						}
    					}
    				}
    
    			}
    		};
    
    		Thread t2 = new Thread("t2") {
    			public void run() {
    				while (true) {
    					synchronized (demo) {
    						System.out.println(Thread.currentThread().getName() + "开始等待");
    						try {
    							demo.wait();
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
    					}
    					System.out.println(Thread.currentThread().getName() + "跳出等待");
    					System.out.println("demo.count" + demo.count);
    					break;
    				}
    
    			}
    		};
    		
    		Thread t3 = new Thread("t3") {
    			public void run() {
    				while (true) {
    					synchronized (demo) {
    						System.out.println(Thread.currentThread().getName() + "开始等待");
    						try {
    							demo.wait();
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
    					}
    					System.out.println(Thread.currentThread().getName() + "跳出等待");
    					break;
    				}
    
    			}
    		};
    		t2.start();
    		t3.start();
    		try {
    			Thread.sleep(100);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		t1.start();
    		
    	}
    }
    

      

    运行结果:

    t2开始等待
    t3开始等待
    t1发出通知
    t3跳出等待
    t2跳出等待
    demo.count99
    

      

  • 相关阅读:
    添加常驻Notification
    Java 数组操作
    一百本英文原著之旅 ( 15 finished )
    SQLServer2005中查询语句的执行顺序
    高效程序员的45个习惯
    博客园经典闪存语录
    for xml path('') 引发的数据不完整
    ajax向前台输出二维数组 并解析
    重视知识的本质
    C语言排序
  • 原文地址:https://www.cnblogs.com/warrior4236/p/7532158.html
Copyright © 2020-2023  润新知