-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiThreading.java
More file actions
44 lines (43 loc) · 1.11 KB
/
multiThreading.java
File metadata and controls
44 lines (43 loc) · 1.11 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
public class multiThreading {
public static void main(String[]args)
{
//MultiThrreading= enables program to run multiple threads concurrently
Thread t1 =new Thread(new Myrun("ping"));
Thread t2 =new Thread(new Myrun("pong"));
System.out.println("GAME START!");
t1.start();
t2.start();
try{
t1.join();
t2.join();
}
catch(InterruptedException e)
{
System.out.println("Thread got interrupted");
}
catch(NoClassDefFoundError e)
{
System.out.println("Thread got interrupted");
}
System.out.println("GAME over!");
}
}
class Myrun implements Runnable {
private final String text;
Myrun(String text)
{
this.text=text;
}
@Override
public void run(){
for(int i = 1;i<=5;i++)
{
try {
Thread.sleep(1000);
System.out.println(text);
} catch (InterruptedException e) {
System.out.println("Thread got interrupted");
}
}
}
}