-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadEx3.java
More file actions
42 lines (36 loc) · 970 Bytes
/
ThreadEx3.java
File metadata and controls
42 lines (36 loc) · 970 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
//Directly use Runnable Interface
//We avoid using Thread class, we manually create Threads here!
//Now a class can Inherit another class and implement an Runnable Interface for Thread behaviour!
class A implements Runnable{
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hi");
try {
Thread.sleep(10);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
class B implements Runnable{
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hello");
try {
Thread.sleep(10);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
}
public class ThreadEx3{
public static void main(String args[]){
//Runnable Objects
Runnable objA = new A();
Runnable objB = new B();
//Create Threads manually
Thread t1 = new Thread(objA); // Thread constructor accepts Runnable Obj! Pass them accordingly!
Thread t2 = new Thread(objB);
//Trigger the Threads!
t1.start();
t2.start();
}
}