-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
43 lines (37 loc) · 1.08 KB
/
Example2.java
File metadata and controls
43 lines (37 loc) · 1.08 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
package lambda_expressions;
/*
* Example demonstrating concrete class implementation, anonymous class implementation and
* lambda expression implementation of Runnable interface for Thread creation.
*/
class RunnableImpl implements Runnable{
@Override
public void run() {
for (int i=0; i<10; i++){
System.out.println("Thread 1: "+i);
}
}
}
public class Example2 {
public static void main(String[] args) {
// using implementation class of Runnable
Thread t1 = new Thread ( new RunnableImpl() );
// using anonymous inner class
Thread t2 = new Thread( new Runnable (){
@Override
public void run() {
for (int i=0; i<10; i++) {
System.out.println("Thread 2: " + i);
}
}
});
//using lambda expression
Thread t3 = new Thread( () -> {
for (int i=0; i<10; i++){
System.out.println("Thread 3: "+i);
}
});
t1.start();
t2.start();
t3.start();
}
}