-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread demo.txt
48 lines (38 loc) · 845 Bytes
/
Thread demo.txt
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
What are threads?
Threads are a virtual CPU.
The three parts of at thread are:
• CPU
• Code
• Data
=========================================
Class HelloRunner is a user defined java class
keyword
implements interface name -> extending the interface
methods should be defined
interface name ->Runnable
interface method ->run() defined
how many threads are there in this code
1. main thread is a default
2. child thread
when ever we create a thread it is always
child thread
public class ThreadTester {
public static void main(String args[]) {
HelloRunner r = new HelloRunner();
Thread t = new Thread(r);
t.start();
}
}
class HelloRunner implements Runnable {
int i;
public void run() {
i = 0;
while (true) {
System.out.println("Hello " + i++);
if ( i == 50 ) {
break;
}
}
}
}
========================================