-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multithreading.java
86 lines (86 loc) · 1.7 KB
/
Multithreading.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class IntegerThread implements Runnable
{
String name;
Thread t;
IntegerThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
t.start();
}
@Override
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Thread 1: "+i);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Thread 1 interupted");
}
}
System.out.println("Exiting THREAD 1");
}
}
class CharacterThread implements Runnable
{
String name;
Thread t;
CharacterThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
t.start();
}
@Override
public void run()
{
for(int i=65;i<=69;i++)
{
System.out.println("Thread 2: "+((char)i));
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Thread 2 interupted");
}
}
System.out.println("Exiting THREAD 2");
}
}
public class Multithreading
{
public static void main(String[] args)
{
System.out.println("Entering MAIN THREAD");
IntegerThread ob1 = new IntegerThread("Thread 1");
CharacterThread ob2 = new CharacterThread("Thread 2");
try
{
Thread.sleep(1000);
ob1.t.suspend();
System.out.println("Thread 1 suspended");
Thread.sleep(2000);
System.out.println("Resuming Thread 1");
ob1.t.resume();
ob2.t.suspend();
System.out.println("Thread 2 suspended");
Thread.sleep(2000);
System.out.println("Resuming Thread 2");
ob2.t.resume();
ob1.t.join();
ob2.t.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread interupted");
}
System.out.println("Exiting MAIN THREAD");
}
}