-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadingDemo2.java
More file actions
62 lines (48 loc) · 1.06 KB
/
ThreadingDemo2.java
File metadata and controls
62 lines (48 loc) · 1.06 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
61
62
package section15;
import java.util.Random;
class MyCounter2 implements Runnable {
private int threadNo;
public MyCounter2(int threadNo) {
this.threadNo = threadNo;
}
@Override
public void run() {
Random random = new Random();
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(random.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("the value of i is :" + i + " thread no: " + threadNo);
}
}
}
public class ThreadingDemo2 {
public static void main(String[] args) {
/*
* Thread thread = new Thread(new MyCounter2(1));
*
* Thread thread2 = new Thread(new MyCounter2(2));
*
* thread.run();
*
* thread2.run();
*/
// another way of creating thread, anonymous way
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(900);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}).start();
}
}