-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadDemo2E.java
59 lines (53 loc) · 1.32 KB
/
ThreadDemo2E.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Demonstrates the sleep method
public class ThreadDemo2E
{
public static void main(String[] args)
{
System.out.println(Thread.currentThread().getName() + " BEGIN");
//Creating 3 new threads
Thread t1 = new Thread(new Greeting5());
Thread t2 = new Thread(new Greeting5());
Thread t3 = new Thread(new Greeting5());
//Starts those threads
t1.start();
t2.start();
t3.start();
try
{
if(t1.isAlive())
{
t1.join();
}
if(t2.isAlive())
{
t2.join();
}
if(t3.isAlive())
{
t3.join();
}
}
catch(InterruptedException ex)
{
}
System.out.println(Thread.currentThread().getName() + " FINISHED");
}
}
class Greeting5 implements Runnable
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println(Thread.currentThread().getName() + " says Hello" + i);
try
{
Thread.sleep(4000);
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
};
}