Skip to content

Commit 6862333

Browse files
author
chenweijie
committed
git add chapter7
1 parent 9c15c81 commit 6862333

File tree

17 files changed

+392
-1
lines changed

17 files changed

+392
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.api.util.thread.study.chapter7.groupInnerStop;
2+
3+
4+
/**
5+
* @author : chen weijie
6+
* @Date: 2018-05-30 00:47
7+
*/
8+
public class MyThread extends Thread {
9+
10+
11+
public MyThread(ThreadGroup group, String name) {
12+
super(group, name);
13+
}
14+
15+
@Override
16+
public void run() {
17+
18+
System.out.println("threadName:" + Thread.currentThread().getName() + "准备开始循环了");
19+
while (!Thread.currentThread().isInterrupted()) {
20+
21+
}
22+
23+
System.out.println("threadName:" + Thread.currentThread().getName() + "结束循环了。");
24+
25+
}
26+
27+
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.chen.api.util.thread.study.chapter7.groupInnerStop;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-30 00:50
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
ThreadGroup threadGroup = new ThreadGroup("我的线程组");
11+
12+
try {
13+
for (int i = 0; i < 10; i++) {
14+
MyThread thread = new MyThread(threadGroup, "线层:" + i);
15+
thread.start();
16+
}
17+
Thread.sleep(5000);
18+
threadGroup.interrupt();
19+
System.out.println("调用了interrupt方法。");
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
24+
}
25+
26+
27+
}
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.chapter7.stateTest2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-30 00:01
6+
*/
7+
public class MyService {
8+
9+
synchronized public static void serviceMethod() {
10+
try {
11+
System.out.println("线程:" + Thread.currentThread().getName() + "调用serviceMethod方法");
12+
Thread.sleep(10000);
13+
} catch (InterruptedException e) {
14+
e.printStackTrace();
15+
}
16+
}
17+
18+
19+
20+
21+
}
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.chapter7.stateTest2;
2+
3+
4+
/**
5+
* @author : chen weijie
6+
* @Date: 2018-05-30 00:03
7+
*/
8+
public class Run {
9+
10+
public static void main(String[] args) {
11+
12+
Thread1 thread1 = new Thread1();
13+
thread1.setName("1");
14+
thread1.start();
15+
16+
Thread2 thread2 = new Thread2();
17+
thread2.setName("2");
18+
thread2.start();
19+
20+
System.out.println("thread2的状态:" + thread2.getState());
21+
22+
23+
}
24+
25+
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.chen.api.util.thread.study.chapter7.stateTest2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-30 00:11
6+
*/
7+
public class Thread1 extends Thread {
8+
9+
10+
11+
@Override
12+
public void run(){
13+
14+
MyService.serviceMethod();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.chen.api.util.thread.study.chapter7.stateTest2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-30 00:11
6+
*/
7+
public class Thread2 extends Thread {
8+
9+
10+
11+
@Override
12+
public void run(){
13+
14+
MyService.serviceMethod();
15+
}
16+
}
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.chapter7.stateTest3;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-29 23:43
6+
*/
7+
public class MyThread extends Thread {
8+
9+
10+
@Override
11+
public void run() {
12+
try {
13+
System.out.println("begin sleep");
14+
Thread.sleep(10000);
15+
System.out.println("end sleep");
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
20+
}
21+
22+
23+
}
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.chapter7.stateTest3;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-29 23:45
6+
*/
7+
public class Run {
8+
9+
public static void main(String[] args) {
10+
11+
try {
12+
MyThread thread = new MyThread();
13+
thread.start();
14+
Thread.sleep(1000);
15+
System.out.println("thread state:" + thread.getState());
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
20+
21+
}
22+
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.chen.api.util.thread.study.chapter7.stateTest4;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-30 00:18
6+
*/
7+
public class Lock {
8+
9+
public static final Byte lock = new Byte("0");
10+
11+
}
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.chapter7.stateTest4;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2018-05-30 00:19
6+
*/
7+
public class MyThread extends Thread {
8+
9+
@Override
10+
public void run() {
11+
12+
synchronized (Lock.lock) {
13+
try {
14+
Lock.lock.wait();
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)