Skip to content

Commit df3ed1d

Browse files
committed
添加ThreadTutorial到2.2.10
1 parent 269adff commit df3ed1d

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.byhieg.threadtutorial.char02;
2+
3+
/**
4+
* Created by byhieg on 17/1/3.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class StaticService {
8+
9+
synchronized public static void printA(){
10+
try{
11+
System.out.println(" 线程名称为:" + Thread.currentThread().getName()
12+
+ " 在 " + System.currentTimeMillis() + " 进入printA");
13+
Thread.sleep(1000 * 3);
14+
System.out.println(" 线程名称为:" + Thread.currentThread().getName()
15+
+ " 在 " + System.currentTimeMillis() + " 离开printA");
16+
}catch (InterruptedException e){
17+
e.printStackTrace();
18+
}
19+
}
20+
21+
synchronized public static void printB(){
22+
System.out.println(" 线程名称为:" + Thread.currentThread().getName()
23+
+ " 在 " + System.currentTimeMillis() + " 进入printB");
24+
System.out.println(" 线程名称为:" + Thread.currentThread().getName()
25+
+ " 在 " + System.currentTimeMillis() + " 离开printB");
26+
}
27+
28+
synchronized public void printC(){
29+
System.out.println(" 线程名称为:" + Thread.currentThread().getName()
30+
+ " 在 " + System.currentTimeMillis() + " 进入printC");
31+
System.out.println(" 线程名称为:" + Thread.currentThread().getName()
32+
+ " 在 " + System.currentTimeMillis() + " 离开printC");
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.byhieg.threadtutorialtest.char02test;
2+
3+
import cn.byhieg.threadtutorial.char02.StaticService;
4+
import junit.framework.TestCase;
5+
6+
import javax.management.relation.RoleUnresolved;
7+
8+
/**
9+
* Created by byhieg on 17/1/3.
10+
* Mail to byhieg@gmail.com
11+
*/
12+
public class StaticServiceTest extends TestCase {
13+
14+
public void testPrint() throws Exception{
15+
new Thread(new Runnable() {
16+
public void run() {
17+
StaticService.printA();
18+
}
19+
}).start();
20+
21+
new Thread(new Runnable() {
22+
public void run() {
23+
StaticService.printB();
24+
}
25+
}).start();
26+
27+
new Thread(new Runnable() {
28+
public void run() {
29+
new StaticService().printC();
30+
}
31+
}).start();
32+
33+
Thread.sleep(1000 * 3);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)