-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathTestEvenOddThread.java
More file actions
43 lines (43 loc) · 918 Bytes
/
Copy pathTestEvenOddThread.java
File metadata and controls
43 lines (43 loc) · 918 Bytes
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
//28
class RunnableDemo implements Runnable{
private Thread t;
private String threadName;
RunnableDemo(String name){
threadName=name;
System.out.println("Creating "+ threadName);
}
public void run(){
System.out.println("Running "+ threadName);
try{
for(int i=1;i<=50;i++){
if(threadName.equals("EVEN")){
Thread.sleep(100);
if(i%2==0)
System.out.println(threadName+" - "+(i));
}
else{
Thread.sleep(100);
if(i%2!=0)
System.out.println(threadName+" - "+(i));
}
}
}catch(InterruptedException e){
System.out.println("Thread " + threadName + " interrupted");
}
System.out.println("Thread " + threadName + " existing");
}
public void start(){
if(t==null){
t=new Thread(this,threadName);
t.start();
}
}
}
class TestEvenOddThread{
public static void main(String args[]){
RunnableDemo r1 = new RunnableDemo("EVEN");
r1.start();
RunnableDemo r2 = new RunnableDemo("ODD");
r2.start();
}
}