An example of the join () method is as follows:
1 Public classthread1{2 Public Static voidMain (string[] args) {3Testthread t=NewTestthread ("T");4 T.start ();5 Try{6T.join ();//similar to calling the run () method.7}Catch(interruptedexception e) {8 9 }Ten for(inti=0;i<=10; i++ ) { OneSystem.out.println ("I am Main thread!!"); A } - } - } the - classTestthreadextendsthread{ - PublicTestthread (String s) { - Super(s); + } - Public voidrun () { + for(inti=0;i<=10;i++) { ASystem.out.println ("I ' M" +getname () + "thread!!"); at Try{ -Sleep (1000);//sleep () must catch Exception. -}Catch(interruptedexception e) { - return; - } - } in } -}
The output is:
1I ' M t thread!!2I ' M t thread!!3I ' M t thread!!4I ' M t thread!!5I ' M t thread!!6I ' M t thread!!7I ' M t thread!!8I ' M t thread!!9I ' M t thread!!TenI ' M t thread!! OneI ' m main thread!! AI ' m main thread!! -I ' m main thread!! -I ' m main thread!! theI ' m main thread!! -I ' m main thread!! -I ' m main thread!! -I ' m main thread!! +I ' m main thread!! -I ' m main thread!!
Yield () Method Example:
1 Public classthread1{2 Public Static voidMain (string[] args) {3Testthread t1=NewTestthread ("T1");4Testthread t2=NewTestthread ("T2");5 T1.start (); T2.start ();6 }7 }8 9 classTestthreadextendsthread{Ten PublicTestthread (String s) { One Super(s); A } - Public voidrun () { - for(inti=1;i<=100;i++) { theSystem.out.println (GetName () + ":" +i); - if(i%10==0){ -Yield ();//when i%10==0, it will yield the CPU. - } + } - } +}
Output: The current thread T1 (T2) will yield the CPU to T2 (T1) whenever i%10==0 is present.
1T1:12T1:23T1:34T1:45T1:56T1:67T1:78T2:19T1:8TenT2:2 OneT1:9 AT2:3 -T1:10 -T2:4 theT1:11 -T2:5 -T2:6 -T1:12 +T1:13 -T2:7 +T2:8 AT2:9 atT2:10 -T1:14 -T1:15 -T1:16 -T1:17 -T1:18 inT1:19 -T1:20 toT2:11 +T2:12 -T2:13 theT2:14 *T2:15 $T2:16Panax NotoginsengT2:17 -T2:18 theT2:19 +T2:20 AT1:21 theT1:22 +T1:23 -T1:24 $T1:25 $T1:26 -T1:27 -T1:28 theT1:29 -T1:30WuyiT2:21 theT2:22 -T2:23 WuT2:24 -T2:25 AboutT2:26 $T2:27 -T2:28 -T2:29 -T2:30 AT2:31 +T2:32 theT2:33 -T2:34 $T2:35 theT2:36 theT2:37 thet2:38 thet2:39 -T2:40 int2:41 theT2:42 thet2:43 AboutT2:44 theT2:45 thet2:46 theT2:47 +t2:48 -t2:49 theT2:50BayiT1:31 theT1:32 theT1:33 -T1:34 -T1:35 theT1:36 theT1:37 thet1:38 thet1:39 -T1:40 theT2:51 thet2:52 thet2:5394t2:54 theT2:55 thet2:56 thet2:5798t2:58 Aboutt2:59 -T2:60101t1:41102T1:42103t1:43104T1:44 theT1:45106t1:46107T1:47108t1:48109t1:49 theT1:50111t2:61 thet2:62113t2:63 theT2:64 theT2:65 thet2:66117t2:67118t2:68119t2:69 -T2:70121t2:71122t2:72123t2:73124t2:74 theT2:75126t2:76127T2:77 -t2:78129t2:79 theT2:80131t2:81 thet2:82133t2:83134t2:84135T2:85136T2:86137t2:87138t2:88139t2:89 $T2:90141T1:51142t1:52143t1:53144t1:54145T1:55146t1:56147t1:57148t1:58149t1:59 MaxT1:60151t2:91 thet2:92153t2:93154t2:94155T2:95156t2:96157t2:97158T2:98159t2:99 thet2:100161t1:61162t1:62163t1:63164T1:64165T1:65166t1:66167t1:67168t1:68169t1:69 theT1:70171t1:71172t1:72173t1:73174t1:74175T1:75176t1:76177T1:77178t1:78179t1:79 theT1:80181t1:81182t1:82183t1:83184t1:84185T1:85186T1:86187t1:87188t1:88189t1:89 theT1:90191t1:91192t1:92193t1:93194t1:94195T1:95196t1:96197t1:97198T1:98199t1:99 $t1:100
View Code
Java thread Sleep (), join (), and yield method