-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaysToImplement.java
More file actions
60 lines (52 loc) · 2.19 KB
/
WaysToImplement.java
File metadata and controls
60 lines (52 loc) · 2.19 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//All these 4 methods does not return value or support exception handling!
//check part 2 of this same file for more info!
class MyThread extends Thread{
@Override
public void run(){
System.out.println("Using Thread class!");
System.out.println("Thread started!");
System.out.println("Thread Ended!\n");
}
}
class MyRunnable implements Runnable{
@Override
public void run(){
System.out.println("Using class that implements Runnable Interface + Thread class");
System.out.println("Thread started!");
System.out.println("Thread Ended!\n");
}
}
public class WaysToImplement {
public static void main(String[] args) {
//Direct implementation for thread using custom class!
//using Thread class
MyThread t = new MyThread();
t.start();
//Whenever we use runnable , we need to create seperate instance for Thread.
//using Runnable Interface(Anonymous class) + Thread class
Runnable tRun = new Runnable() {
@Override
public void run(){
System.out.println("Using Runnable Interface(Anonymous class) + Thread class!");
System.out.println("Thread started!");
System.out.println("Thread Ended!\n");
}
};
Thread t2 = new Thread(tRun);
t2.start();
//Whenever we use runnable , we need to create seperate instance for Thread.
//using Class that implements Runnable interface + Thread class
Thread t3 = new Thread(new MyRunnable());
t3.start();
//using lambda expressions to create threads
//if using lambda expressions(This can be only used for functionalInterface!)
//Functional Interface -> Interface that contains only one method
Runnable tRun2 = () -> { //we know that this Runnable has only run() method to override! (So we can use lambda here!)
System.out.println("Using Lambda Expressions to implement Runnable Interface + Thread class");
System.out.println("Thread Started!");
System.out.println("Thread Ended!\n");
};
Thread t4 = new Thread(tRun2);
t4.start();
}
}