-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadEx4.java
More file actions
40 lines (35 loc) · 1.09 KB
/
ThreadEx4.java
File metadata and controls
40 lines (35 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
//same as prev file but using Anonymous class and Lambda functions
//Runnable is an functional Interface!
public class ThreadEx4{
public static void main(String args[]){
//Runnable Objects
//Using Anonymous class!
Runnable objA = new Runnable(){ // instantiate an Interface with anonymous class!
public void run(){
for(int i=1;i<=5;i++){
System.out.println("Hi");
try {
Thread.sleep(10);
} catch (InterruptedException e) { e.printStackTrace();}
}
}
};
//Using Lambda Function (Works only for Functional Interface!)
Runnable objB = () -> // cool syntax for functional interface!
//since FI only has one method...we can give body directly!
{ // start of body
for(int i=1;i<=5;i++){
System.out.println("Hello");
try {
Thread.sleep(10);
} catch (InterruptedException e) { e.printStackTrace();}
}
}; //end of body!
//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();
}
}