码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-16 20:23:38      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:对象   通知   常用   package   get   private   str   author   ini   

一:概念

线程通信中经常用到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

  

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

标签:对象   通知   常用   package   get   private   str   author   ini   

原文地址:http://www.cnblogs.com/warrior4236/p/7532158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!