Skip to content

Commit

Permalink
java多线程_wait()、notify()方法
Browse files Browse the repository at this point in the history
  • Loading branch information
1263919792@qq.com committed Jul 1, 2019
1 parent b353304 commit 5eaa1cf
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/main/java/thread_demo/demo33/Run.java
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();
}
}
28 changes: 28 additions & 0 deletions src/main/java/thread_demo/demo33/Service.java
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();
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/thread_demo/demo33/ThreadA.java
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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/thread_demo/demo33/ThreadB.java
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);
}
}
16 changes: 16 additions & 0 deletions src/main/java/thread_demo/demo33/ThreadC.java
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);
}
}
4 changes: 3 additions & 1 deletion src/main/java/thread_demo/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ demo30 : volatile只能保证变量的可见性,不能保证操作的原子性

demo31 : AtomicInteger : 原子类,保证以原子的方式更新int值.

demo32 : 原子类只能保证操作的原子性,不能保证其有序性。
demo32 : 原子类只能保证操作的原子性,不能保证其有序性。

demo33 : Object类的wait()、notify()方法在线程中的使用。

0 comments on commit 5eaa1cf

Please sign in to comment.