Skip to content

Commit 36bfaf7

Browse files
update ThreadIsCool class
1 parent 69675da commit 36bfaf7

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed
Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
11
package multithread;
22

3-
/** This is a cool and small program to show that threads don't run in the order that you can control, it's all scheduled by the thing called
4-
* Thread Scheduler.*/
5-
6-
public class ThreadIsCool implements Runnable{
7-
8-
public static void main(String [] args){
9-
ThreadIsCool threadIsCool = new ThreadIsCool();
10-
Thread thread1 = new Thread(threadIsCool);
11-
Thread thread2 = new Thread(threadIsCool);
12-
Thread thread3 = new Thread(threadIsCool);
13-
thread1.setName("Thread Andrew");
14-
thread2.setName("Thread James");
15-
thread3.setName("Thread Steve");
16-
System.out.println("Now the three threads kick off:");
17-
18-
thread1.start();
19-
try {
20-
/* Wait for this thread to die before other invocations*/
3+
/**
4+
* This is a cool and small program to show that threads run order could be controlled by using Thread.join() method.
5+
*/
6+
7+
public class ThreadIsCool implements Runnable {
8+
9+
public static void main(String[] args) {
10+
ThreadIsCool threadIsCool = new ThreadIsCool();
11+
Thread thread1 = new Thread(threadIsCool);
12+
Thread thread2 = new Thread(threadIsCool);
13+
Thread thread3 = new Thread(threadIsCool);
14+
Thread thread4 = new Thread(threadIsCool);
15+
thread1.setName("Thread 1");
16+
thread2.setName("Thread 2");
17+
thread3.setName("Thread 3");
18+
thread4.setName("Thread 4");
19+
System.out.println("Now all the threads are about to kick off:");
20+
21+
thread1.start();
22+
try {
23+
/* Wait for this thread to die before other invocations*/
2124
thread1.join();
2225
} catch (InterruptedException e) {
2326
e.printStackTrace();
2427
}
25-
26-
thread2.start();
27-
try {
28-
/* Wait for this thread to die before other invocations*/
28+
29+
thread2.start();
30+
try {
31+
/* Wait for this thread to die before other invocations*/
2932
thread2.join();
3033
} catch (InterruptedException e) {
3134
e.printStackTrace();
3235
}
33-
34-
thread3.start();
35-
try {
36-
/* Wait for this thread to die before other invocations*/
36+
37+
thread3.start();
38+
try {
39+
/* Wait for this thread to die before other invocations*/
3740
thread3.join();
3841
} catch (InterruptedException e) {
3942
e.printStackTrace();
4043
}
41-
System.out.println("Now the Program ended.");
42-
}
43-
44-
@Override
45-
public void run(){
46-
for(int i = 0; i < 10; i++){
47-
String threadName = Thread.currentThread().getName();
48-
System.out.println(threadName + " is running!");
49-
}
50-
System.out.println(Thread.currentThread().getName() + " is sleeping for 3 seconds");
51-
try {
52-
Thread.sleep(3000);
44+
45+
thread4.start();
46+
try {
47+
/* Wait for this thread to die before other invocations*/
48+
thread4.join();
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
System.out.println("Now the Program ended.");
53+
}
54+
55+
@Override
56+
public void run() {
57+
for (int i = 0; i < 10; i++) {
58+
String threadName = Thread.currentThread().getName();
59+
System.out.println(threadName + " is running!");
60+
}
61+
System.out.println(Thread.currentThread().getName() + " is sleeping for 1 second");
62+
try {
63+
Thread.sleep(1000);
5364
} catch (InterruptedException e) {
5465
e.printStackTrace();
5566
}
56-
System.out.println(Thread.currentThread().getName() + " run finished.");
57-
}
58-
67+
System.out.println(Thread.currentThread().getName() + " run finished.");
68+
}
69+
5970
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy