Thread的wait和notify是指的是 Runnable物件而不是 Thread物件,切記,否則喚醒失效
package com.wanju.project001.zonghe.test;
public class TestThreadNotifyWait {
public static void main(String[] args) {
T1 rt1=new T1();
Thread t1 = new Thread(rt1);
Thread t2 = new Thread(new T2(t1));
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// t2.start();
synchronized (rt1) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----"
+ i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rt1.notify();
}
// int i=0;
// synchronized (rt1) {
// while (++i <= 10) {
// try {
// Thread.sleep(500);
// System.out.println("The main thread.-->" + i);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// rt1.notify();
// }
}
}
class T1 implements Runnable {
public void run() {
// synchronized (this) {
// for (int i = 0; i < 10; i++) {
// if (i == 4) {
//
// try {
// this.wait();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// // this.notifyAll();
// }
// System.out.println(Thread.currentThread().getName() + "----"
// + i);
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
int i = 0;
// 使用物件本身this作為監視器(與主執行緒的監視器為同一個物件)
synchronized (this) {
while (i++ < 20) {
try {
Thread.sleep(500);
System.out.println("The sub thread.-->" + i);
if (i == 10) {
// 釋放監視器鎖,阻塞等待...
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class T2 implements Runnable {
Thread t1;
public T2() {
// TODO Auto-generated constructor stub
}
public T2(Thread t1) {
this.t1 = t1;
}
public void run() {
synchronized (t1) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----"
+ i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t1.notify();
}
}
}
public class TestThreadNotifyWait {
public static void main(String[] args) {
T1 rt1=new T1();
Thread t1 = new Thread(rt1);
Thread t2 = new Thread(new T2(t1));
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// t2.start();
synchronized (rt1) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----"
+ i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rt1.notify();
}
// int i=0;
// synchronized (rt1) {
// while (++i <= 10) {
// try {
// Thread.sleep(500);
// System.out.println("The main thread.-->" + i);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// rt1.notify();
// }
}
}
class T1 implements Runnable {
public void run() {
// synchronized (this) {
// for (int i = 0; i < 10; i++) {
// if (i == 4) {
//
// try {
// this.wait();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// // this.notifyAll();
// }
// System.out.println(Thread.currentThread().getName() + "----"
// + i);
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
int i = 0;
// 使用物件本身this作為監視器(與主執行緒的監視器為同一個物件)
synchronized (this) {
while (i++ < 20) {
try {
Thread.sleep(500);
System.out.println("The sub thread.-->" + i);
if (i == 10) {
// 釋放監視器鎖,阻塞等待...
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class T2 implements Runnable {
Thread t1;
public T2() {
// TODO Auto-generated constructor stub
}
public T2(Thread t1) {
this.t1 = t1;
}
public void run() {
synchronized (t1) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----"
+ i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t1.notify();
}
}
}
相關文章
- thread的notify和wait怎麼玩?threadAI
- Thread和Runnable的區別thread
- Thread 和runnable的區別thread
- Thread是如何處理Runnable的thread
- Java中Runnable和Thread的區別Javathread
- Java 中Thread 和Runnable的區別Javathread
- 併發-2-Thread和Runnable的APIthreadAPI
- java基礎知識回顧之java Thread類學習(七)--java多執行緒通訊等待喚醒機制(wait和notify,notifyAll)...Javathread執行緒AI
- Java中Thread 和Runnable 深入理解Javathread
- java執行緒系列---Runnable和Thread的區別Java執行緒thread
- Thread(Runnable xxx)中的問題thread
- Thread 的sleep、wait、yield、interruptthreadAI
- 請問jive中所說的thread指的是什麼?thread
- 【胡思亂想】命令模式 與 Thread Runnable模式thread
- JAVA多執行緒Thread VS Runnable詳解Java執行緒thread
- 使用Thread類和Runnable介面實現多執行緒的區別thread執行緒
- Android中Handler Runnable與Thread的區別詳解Androidthread
- wait()和notify()、notifyAll()AI
- 程式控制:程式的建立、終止、阻塞、喚醒和切換
- 大話Android多執行緒(一) Thread和Runnable的聯絡和區別Android執行緒thread
- wait、notify和notifyAll的關係AI
- wait() and notify()AI
- Java 中的 Wait 和 Notify 機制JavaAI
- Reactor中的Thread和SchedulerReactthread
- 物件包裝器類之間的比較用equals()而不是==物件
- 如何檢視失效的物件物件
- Java的wait(), notify()和notifyAll()使用心得JavaAI
- Threadthread
- java多執行緒程式設計,Thread,Runnable,Future相關知識Java執行緒程式設計thread
- h5喚醒APP小記H5APP
- 證明arguments是個物件不是陣列物件陣列
- oracle編譯物件失效Oracle編譯物件
- Java-併發-wait()、notify()和notifyAll()JavaAI
- sleep & wait | notify | notifyAllAI
- Java多執行緒的wait()和notify()例子Java執行緒AI
- 內建物件、宿主物件和本地物件是什麼物件
- 虛假喚醒
- [譯] 為什麼我更喜歡物件而不是switch語句物件