Skip to content

Commit b2c93f5

Browse files
committed
多线程学习
1 parent 9a3fcd1 commit b2c93f5

File tree

32 files changed

+836
-0
lines changed

32 files changed

+836
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyHoldLock;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:16 AM
6+
*/
7+
public class NotifyThread extends Thread {
8+
9+
10+
private Object lock;
11+
12+
public NotifyThread(Object lock) {
13+
this.lock = lock;
14+
}
15+
16+
@Override
17+
public void run() {
18+
Service service = new Service();
19+
service.synNotifyMethod(lock);
20+
}
21+
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyHoldLock;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:10 AM
6+
*/
7+
public class Service {
8+
9+
10+
public void testMethod(Object lock) {
11+
try {
12+
synchronized (lock) {
13+
System.out.println("begin wait(),threadName==" + Thread.currentThread().getName());
14+
lock.wait();
15+
System.out.println("end wait(),threadName==" + Thread.currentThread().getName());
16+
}
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
23+
public void synNotifyMethod(Object lock) {
24+
try {
25+
synchronized (lock) {
26+
System.out.println("begin notify()==" + Thread.currentThread().getName() + ",time==" + System.currentTimeMillis());
27+
Thread.sleep(5000);
28+
System.out.println("end notify()==" + Thread.currentThread().getName() + ",time==" + System.currentTimeMillis());
29+
}
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
35+
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyHoldLock;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:18 AM
6+
*/
7+
public class SynNotifyMethodThread extends Thread {
8+
9+
private Object lock;
10+
11+
public SynNotifyMethodThread(Object lock) {
12+
this.lock = lock;
13+
}
14+
15+
@Override
16+
public void run() {
17+
Service service = new Service();
18+
service.synNotifyMethod(lock);
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyHoldLock;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:20 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) {
10+
Object lock = new Object();
11+
ThreadA a = new ThreadA(lock);
12+
a.start();
13+
14+
NotifyThread notifyThread = new NotifyThread(lock);
15+
notifyThread.start();
16+
17+
SynNotifyMethodThread notifyMethodThread = new SynNotifyMethodThread(lock);
18+
notifyMethodThread.start();
19+
}
20+
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyHoldLock;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author chen weijie
7+
* @date 2018-05-08 12:15 AM
8+
*/
9+
public class ThreadA extends Thread {
10+
11+
12+
private Object lock;
13+
14+
public ThreadA(Object lock) {
15+
this.lock = lock;
16+
}
17+
18+
@Override
19+
public void run() {
20+
Service service = new Service();
21+
service.testMethod(lock);
22+
}
23+
24+
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyOne;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:41 AM
6+
*/
7+
public class NotifyThread extends Thread {
8+
9+
10+
private Object lock;
11+
12+
public NotifyThread(Object lock) {
13+
this.lock = lock;
14+
}
15+
16+
17+
@Override
18+
public void run() {
19+
20+
synchronized (lock) {
21+
// lock.notify();
22+
lock.notifyAll();
23+
}
24+
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyOne;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:40 AM
6+
*/
7+
public class Service {
8+
9+
public void testMethod(Object lock) {
10+
11+
try {
12+
synchronized (lock) {
13+
System.out.println("begin wait,threadName" + Thread.currentThread().getName());
14+
lock.wait();
15+
System.out.println("end wait,threadName" + Thread.currentThread().getName());
16+
}
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyOne;
2+
3+
/**
4+
* @author chen weijie
5+
* @date 2018-05-08 12:43 AM
6+
*/
7+
public class Test {
8+
9+
public static void main(String[] args) throws InterruptedException {
10+
11+
Object o = new Object();
12+
ThreadA a = new ThreadA(o);
13+
a.start();
14+
15+
ThreadB b = new ThreadB(o);
16+
b.start();
17+
18+
ThreadC c = new ThreadC(o);
19+
c.start();
20+
Thread.sleep(5000);
21+
22+
NotifyThread notifyThread = new NotifyThread(o);
23+
notifyThread.start();
24+
}
25+
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyOne;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-05-07 11:58 PM
7+
*/
8+
public class ThreadA extends Thread {
9+
10+
11+
private Object lock;
12+
13+
public ThreadA(Object lock) {
14+
this.lock = lock;
15+
}
16+
17+
@Override
18+
public void run() {
19+
Service service = new Service();
20+
service.testMethod(lock);
21+
}
22+
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chen.api.util.thread.study.chapter3.notifyOne;
2+
3+
4+
/**
5+
* @author chen weijie
6+
* @date 2018-05-07 11:58 PM
7+
*/
8+
public class ThreadB extends Thread {
9+
10+
11+
private Object lock;
12+
13+
public ThreadB(Object lock) {
14+
this.lock = lock;
15+
}
16+
17+
@Override
18+
public void run() {
19+
Service service = new Service();
20+
service.testMethod(lock);
21+
}
22+
23+
24+
}

0 commit comments

Comments
 (0)