-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread9.java
More file actions
42 lines (29 loc) · 1014 Bytes
/
Thread9.java
File metadata and controls
42 lines (29 loc) · 1014 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
/*
Threads with Runnable interface
Oops you cant use currentThread().getName() method or start() in child class.
Because we are not extending from Thread class
But how can we do this without start() from Thread class ?
Well, we cant. So we need a intance from Thread class and pass our.
(Since Thread class has multipe contructors its okay)
Summanry:
If you implment from Runnable, Runnable doenst have start() to invoke
So you have to create an instance from Threrad class and pass the runnble
into that.
*/
class Printer implements Runnable {
public void run(){
for(int i=0; i<100; i++){
System.out.println("child "+Thread.currentThread().getName()+" "+i);
}
}
}
public class Main{
public static void main(String[] args){
Printer printer = new Printer();
Thread thread = new Thread(printer);
thread.start();
for(int i=0; i<10; i++){
System.out.println("main "+Thread.currentThread().getName()+" "+i);
}
}
}