-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1263919792@qq.com
committed
Jul 1, 2019
1 parent
b353304
commit 5eaa1cf
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package thread_demo.demo33; | ||
|
||
/** | ||
* Object类的wait()、notify()方法 | ||
* 通过一个对象,调用这些方法之前,必须先获得该对象的对象级别锁,只能在用该对象锁修饰的同步方法或者同步代码块中执行。 | ||
* | ||
* wait()执行后,会释放锁。notify()的同步代码执行完后,才会释放锁。唤醒的后续代码会去竞争该锁,执行后续代码。 | ||
*/ | ||
public class Run { | ||
public static void main(String[] args) { | ||
Object lock = new Object(); | ||
ThreadA threadA = new ThreadA(lock); | ||
threadA.start(); | ||
|
||
ThreadB threadB = new ThreadB(lock); | ||
threadB.start(); | ||
|
||
ThreadC threadC = new ThreadC(lock); | ||
threadC.start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package thread_demo.demo33; | ||
|
||
public class Service { | ||
public void testMethod(Object lock) { | ||
try { | ||
synchronized (lock) { | ||
System.out.println("begin wait(), thread name : " + Thread.currentThread().getName()); | ||
lock.wait(); | ||
System.out.println("end wait(), thread name : " + Thread.currentThread().getName()); | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void NotifyMethod(Object lock) { | ||
try { | ||
synchronized (lock) { | ||
System.out.println("begin notify(), thread name : " + Thread.currentThread().getName()); | ||
lock.notify(); | ||
Thread.sleep(5000); | ||
System.out.println("end notify(), thread name : " + Thread.currentThread().getName()); | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package thread_demo.demo33; | ||
|
||
public class ThreadA extends Thread { | ||
|
||
private Object lock; | ||
|
||
public ThreadA(Object lock) { | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
Service service = new Service(); | ||
service.testMethod(lock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package thread_demo.demo33; | ||
|
||
public class ThreadB extends Thread { | ||
|
||
private Object lock; | ||
|
||
public ThreadB(Object lock) { | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
Service service = new Service(); | ||
service.NotifyMethod(lock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package thread_demo.demo33; | ||
|
||
public class ThreadC extends Thread { | ||
|
||
private Object lock; | ||
|
||
public ThreadC(Object lock) { | ||
this.lock = lock; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
Service service = new Service(); | ||
service.NotifyMethod(lock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters