File tree Expand file tree Collapse file tree
main/java/cn/byhieg/threadtutorial/char02
test/java/cn/byhieg/threadtutorialtest/char02test Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments