GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-multithread,communication更新至example5 · yyfullstack/java-learning@26d9caa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • java-multithread/src/main/java/com/brianway/learning/java/multithread/communication
    • example3
      • MyThread.java
      • NotifyThread.java
      • NotifyThread2.java
      • NotifyThread3.java
      • Run3_notifyAll.java
      • Run3_notifyMany.java
      • Run3_notifyOne.java
      • Service.java
    • example4
      • Run4_waitHasParam.java
      • Run4_waitHasParam2.java
    • example5
      • MyRun.java
      • Run5_notify.java
      • Run5_notifyEarly.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/MyThread.java‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
public class MyThread extends Thread {
7+
private Object lock;
8+
9+
public MyThread(Object lock) {
10+
super();
11+
this.lock = lock;
12+
}
13+
14+
@Override
15+
public void run() {
16+
Service service = new Service();
17+
service.testMethod(lock);
18+
}
19+
}
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/NotifyThread.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
public class NotifyThread extends Thread {
7+
private Object lock;
8+
9+
public NotifyThread(Object lock) {
10+
super();
11+
this.lock = lock;
12+
}
13+
14+
@Override
15+
public void run() {
16+
synchronized (lock){
17+
lock.notify();
18+
}
19+
}
20+
}
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/NotifyThread2.java‎

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
public class NotifyThread2 extends Thread {
7+
private Object lock;
8+
9+
public NotifyThread2(Object lock) {
10+
super();
11+
this.lock = lock;
12+
}
13+
14+
@Override
15+
public void run() {
16+
synchronized (lock){
17+
lock.notify();
18+
lock.notify();
19+
lock.notify();
20+
lock.notify();
21+
22+
}
23+
}
24+
}
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/NotifyThread3.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
public class NotifyThread3 extends Thread {
7+
private Object lock;
8+
9+
public NotifyThread3(Object lock) {
10+
super();
11+
this.lock = lock;
12+
}
13+
14+
@Override
15+
public void run() {
16+
synchronized (lock){
17+
lock.notifyAll();
18+
}
19+
}
20+
}
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/Run3_notifyAll.java‎

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* P150
9+
* 唤醒多个线程,(逆序?试了好多次,全部都是刚好逆序的。巧合?机制?)
10+
*/
11+
public class Run3_notifyAll {
12+
public static void main(String[] args) throws InterruptedException {
13+
Object lock = new Object();
14+
MyThread a = new MyThread(lock);
15+
a.start();
16+
MyThread b = new MyThread(lock);
17+
b.start();
18+
MyThread c = new MyThread(lock);
19+
c.start();
20+
21+
Thread.sleep(1000);
22+
23+
NotifyThread3 notifyThread = new NotifyThread3(lock);
24+
notifyThread.start();
25+
}
26+
}
27+
28+
/*
29+
输出:
30+
begin wait(),ThreadName=Thread-0
31+
begin wait(),ThreadName=Thread-1
32+
begin wait(),ThreadName=Thread-2
33+
end wait(),ThreadName=Thread-2
34+
end wait(),ThreadName=Thread-1
35+
end wait(),ThreadName=Thread-0
36+
37+
*/
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/Run3_notifyMany.java‎

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* P150
9+
* 通知多个线程
10+
*/
11+
public class Run3_notifyMany {
12+
public static void main(String[] args) throws InterruptedException {
13+
Object lock = new Object();
14+
MyThread a = new MyThread(lock);
15+
a.start();
16+
MyThread b = new MyThread(lock);
17+
b.start();
18+
MyThread c = new MyThread(lock);
19+
c.start();
20+
21+
Thread.sleep(1000);
22+
23+
NotifyThread2 notifyThread = new NotifyThread2(lock);
24+
notifyThread.start();
25+
}
26+
}
27+
28+
/*
29+
输出:
30+
begin wait(),ThreadName=Thread-0
31+
begin wait(),ThreadName=Thread-2
32+
begin wait(),ThreadName=Thread-1
33+
end wait(),ThreadName=Thread-0
34+
end wait(),ThreadName=Thread-1
35+
end wait(),ThreadName=Thread-2
36+
37+
*/
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/Run3_notifyOne.java‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* P149
9+
* 通知一个线程
10+
*/
11+
public class Run3_notifyOne {
12+
public static void main(String[] args) throws InterruptedException {
13+
Object lock = new Object();
14+
MyThread a = new MyThread(lock);
15+
a.start();
16+
MyThread b = new MyThread(lock);
17+
b.start();
18+
MyThread c = new MyThread(lock);
19+
c.start();
20+
21+
Thread.sleep(1000);
22+
23+
NotifyThread notifyThread = new NotifyThread(lock);
24+
notifyThread.start();
25+
}
26+
}
27+
28+
/*
29+
输出:
30+
begin wait(),ThreadName=Thread-1
31+
begin wait(),ThreadName=Thread-2
32+
begin wait(),ThreadName=Thread-0
33+
end wait(),ThreadName=Thread-1
34+
35+
*/
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example3/Service.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.communication.example3;
2+
3+
import java.awt.font.TextHitInfo;
4+
5+
/**
6+
* Created by brian on 2016/4/14.
7+
*/
8+
public class Service {
9+
public void testMethod(Object lock){
10+
try {
11+
synchronized (lock){
12+
System.out.println("begin wait(),ThreadName="+Thread.currentThread().getName());
13+
lock.wait();
14+
System.out.println("end wait(),ThreadName="+Thread.currentThread().getName());
15+
}
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
}
20+
}
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example4/Run4_waitHasParam.java‎

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.brianway.learning.java.multithread.communication.example4;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* p151
9+
* wait(long)使用,超时自动唤醒
10+
*/
11+
public class Run4_waitHasParam {
12+
static private Object lock = new Object();
13+
static private Runnable runnable1 =new Runnable() {
14+
public void run() {
15+
try {
16+
synchronized (lock){
17+
System.out.println("wait begin timer="+System.currentTimeMillis());
18+
lock.wait(4000);
19+
System.out.println("wait end timer="+System.currentTimeMillis());
20+
}
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
};
26+
27+
public static void main(String[] args) {
28+
Thread t = new Thread(runnable1);
29+
t.start();
30+
}
31+
}
32+
33+
34+
/*
35+
输出:
36+
wait begin timer=1460566545742
37+
wait end timer=1460566549743
38+
*/
Collapse file

‎java-multithread/src/main/java/com/brianway/learning/java/multithread/communication/example4/Run4_waitHasParam2.java‎

Copy file name to clipboard
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.brianway.learning.java.multithread.communication.example4;
2+
3+
/**
4+
* Created by brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* p152
9+
* wait(long)使用,时间限制内由其他线程唤醒
10+
*/
11+
public class Run4_waitHasParam2 {
12+
static private Object lock = new Object();
13+
static private Runnable runnable1 =new Runnable() {
14+
public void run() {
15+
try {
16+
synchronized (lock){
17+
System.out.println("wait begin timer="+System.currentTimeMillis());
18+
lock.wait(5000);
19+
System.out.println("wait end timer="+System.currentTimeMillis());
20+
}
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
};
26+
27+
static private Runnable runnable2 =new Runnable() {
28+
public void run() {
29+
30+
synchronized (lock){
31+
System.out.println("notify begin timer="+System.currentTimeMillis());
32+
lock.notify();
33+
System.out.println("notify end timer="+System.currentTimeMillis());
34+
}
35+
36+
}
37+
};
38+
39+
40+
public static void main(String[] args) throws InterruptedException {
41+
Thread t1 = new Thread(runnable1);
42+
t1.start();
43+
Thread.sleep(2000);
44+
Thread t2 = new Thread(runnable2);
45+
t2.start();
46+
}
47+
}
48+
49+
50+
/*
51+
输出:
52+
wait begin timer=1460566507815
53+
notify begin timer=1460566509815
54+
notify end timer=1460566509815
55+
wait end timer=1460566509815
56+
*/

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.