JavaSE 线程通信 wait() notify()
2014-04-21 02:14
471 查看
wait() 和notify()都是Object类的方法,实现thread和另外thread的通信。
主要要点:
一、当thread执行了wait()方法时,会释放掉它所持的对象锁,进入到wait pool中等待,等待被其他线程notify()后进入到lock pool中等待。在lock pool中获得对象锁后,继续从wait()的下一句执行。
二、notify()和wait()都在synchronized中。
三、wait()和sleep()的区别:
a) sleep()是Thread类的函数,wait()是所有类的对象都能obj,wait()
b) sleep()不释放对象锁,休眠若干毫秒后醒来继续执行;wait()会释放掉对象锁,it will lose ownership of lock.
实例:
用wait()和notify()实现4个threads之间的通信。 保证在0和1之间切换,当numberCount为0时,某线程+1操作,变为1; 当numberCount为1时,某线程-1操作,变为0.。。。。。一直在0、1之间跳转,不能跳到-1或2. 实例如下:
package com.syncronized.java;
public class TestNofity {
public static void main(String[] args) {
Example ex = new Example();
ThreadIncrase thIn_1 = new ThreadIncrase(ex);
ThreadIncrase thIn_2 = new ThreadIncrase(ex);
ThreadDecrease thDe_1 = new ThreadDecrease(ex);
ThreadDecrease thDe_2 = new ThreadDecrease(ex);
thIn_1.start();
thIn_2.start();
thDe_1.start();
thDe_2.start();
}
}
class Example {
private int numberCount;
public synchronized void increase() {
while(numberCount != 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (numberCount == 0) {
numberCount++;
System.out.println(numberCount);
}
this.notify();
}
public synchronized void decrease() {
while(numberCount == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
numberCount--;
System.out.println(numberCount);
this.notify();
}
}
class ThreadIncrase extends Thread {
private Example example;
public ThreadIncrase(Example exam) {
example = exam;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep((long) (1000 * Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
}
example.increase();
}
}
}
class ThreadDecrease extends Thread {
private Example example;
public ThreadDecrease(Example exam) {
example = exam;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep((long) (1000 * Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
}
example.decrease();
}
}
}
输出:
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
主要要点:
一、当thread执行了wait()方法时,会释放掉它所持的对象锁,进入到wait pool中等待,等待被其他线程notify()后进入到lock pool中等待。在lock pool中获得对象锁后,继续从wait()的下一句执行。
二、notify()和wait()都在synchronized中。
三、wait()和sleep()的区别:
a) sleep()是Thread类的函数,wait()是所有类的对象都能obj,wait()
b) sleep()不释放对象锁,休眠若干毫秒后醒来继续执行;wait()会释放掉对象锁,it will lose ownership of lock.
实例:
用wait()和notify()实现4个threads之间的通信。 保证在0和1之间切换,当numberCount为0时,某线程+1操作,变为1; 当numberCount为1时,某线程-1操作,变为0.。。。。。一直在0、1之间跳转,不能跳到-1或2. 实例如下:
package com.syncronized.java;
public class TestNofity {
public static void main(String[] args) {
Example ex = new Example();
ThreadIncrase thIn_1 = new ThreadIncrase(ex);
ThreadIncrase thIn_2 = new ThreadIncrase(ex);
ThreadDecrease thDe_1 = new ThreadDecrease(ex);
ThreadDecrease thDe_2 = new ThreadDecrease(ex);
thIn_1.start();
thIn_2.start();
thDe_1.start();
thDe_2.start();
}
}
class Example {
private int numberCount;
public synchronized void increase() {
while(numberCount != 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (numberCount == 0) {
numberCount++;
System.out.println(numberCount);
}
this.notify();
}
public synchronized void decrease() {
while(numberCount == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
numberCount--;
System.out.println(numberCount);
this.notify();
}
}
class ThreadIncrase extends Thread {
private Example example;
public ThreadIncrase(Example exam) {
example = exam;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep((long) (1000 * Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
}
example.increase();
}
}
}
class ThreadDecrease extends Thread {
private Example example;
public ThreadDecrease(Example exam) {
example = exam;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep((long) (1000 * Math.random()));
} catch (InterruptedException e) {
e.printStackTrace();
}
example.decrease();
}
}
}
输出:
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
相关文章推荐
- JavaSE第一百零四讲:哲学家就餐问题、死锁与使用wait及notify方法实现线程之间的相互通信
- 使用wait方法和notify方法用于线程间通信的正确姿势
- jvm的线程之间的通信-notify-wait-notifyAll
- Java核心知识点学习----多线程并发之线程间的通信,notify,wait
- java的线程通信wait(),notify(),notifyAll()
- Java基础(高级)——多线程的理解和Synchronized实例,以及线程间通信,wait,notify等方法
- java.util.concurrent包(3)――线程间通信wait/notify和await/signal
- wait和notify实现线程间的通信
- 使用wait/notify实现线程间通信
- wait()与notify()实现线程间的通信
- Java 多线程之线程间的通信——wait及notify方法
- Java【多线程知识总结(10)】线程通信之wait()与notify()的运用--模拟指挥官指挥2个连队交替轰炸战区
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- Java中线程间的通信(synchronized,wait,notify)
- Java使用notify()、wait()、和notifyAll()来进行线程间的通信
- 多线程同步-线程之间通信:wait notify 另外还有interrupt,join方法
- Java 多线程之线程间的通信——wait及notify方法
- 线程间通信 wait()/notify() 用例
- java 通过使用wait和notify进行线程之间通信(代码)
- Java线程通信——wait() 和 notify()