forked from disha2sinha/Object-Oriented-Programming-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreeThread.java
63 lines (61 loc) · 1.47 KB
/
ThreeThread.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
60
61
62
63
class Multithread implements Runnable{
long click=0;
Thread t;
private volatile boolean running =true;
public Multithread(int p)
{
t=new Thread(this);
t.setPriority(p);
t.start();
}
public void run()
{
System.out.println("Thread Running:"+t);
while(running)
{
if(click>10)
{
stop();
}
System.out.println(click++);
}
}
public void stop()
{
running=false;
}
}
/**
* ThreeThread
*/
public class ThreeThread {
public static void main(String[] args) {
Thread.currentThread().setPriority(10);
Multithread th1=new Multithread(7);
Multithread th2=new Multithread(5);
Multithread th3=new Multithread(3);
try{
System.out.println("Sleeping main Thread");
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted:"+e);
}
th1.stop();
th2.stop();
th3.stop();
try{
th1.t.join();
th2.t.join();
th3.t.join();
}
catch(Exception e)
{
System.out.println("Interrupted:"+e);
}
System.out.println("Thread1:"+th1.click);
System.out.println("Thread2:"+th2.click);
System.out.println("Thread3:"+th3.click);
}
}