-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_threads.java
More file actions
81 lines (70 loc) · 1.09 KB
/
multiple_threads.java
File metadata and controls
81 lines (70 loc) · 1.09 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class NewThread extends Thread
{
String name;
Thread t;
boolean suspendflag;
NewThread(String threadname)
{
name = threadname;
t=new Thread(this,name);
System.out.println(t);
suspendflag=false;
t.start();
}
public void run()
{
try
{
for(int i=0;i<50;i++)
{
System.out.println(name + " : "+i);
Thread.sleep(200);
synchronized(this)
{
while (suspendflag)
{
wait();
}
}
}
}catch(InterruptedException e){ }
System.out.println(name + " exiting");
}
void mysuspend()
{
suspendflag=true;
}
synchronized void myresume()
{
suspendflag=false;
notify();
}
}
public class multiple_threads
{
public static void main(String[] arg)
{
NewThread ob1=new NewThread("one");
NewThread ob2=new NewThread("two");
try
{
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Suspending one");
Thread.sleep(1000);
System.out.println("Resume one");
ob1.myresume();
ob2.mysuspend();
System.out.println("Suspending two");
Thread.sleep(1000);
System.out.println("Resume two");
ob2.myresume();
} catch(InterruptedException e){ }
try
{
ob1.t.join();
ob2.t.join();
} catch(InterruptedException e){ }
System.out.println("Main Exiting");
}
}