-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadingDemo.java
More file actions
65 lines (49 loc) · 1.37 KB
/
ThreadingDemo.java
File metadata and controls
65 lines (49 loc) · 1.37 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
63
64
65
package section15;
class MyCounter extends Thread {
private int threadNo;
public MyCounter(int threadNo) {
this.threadNo = threadNo;
}
@Override
public void run() {
countMe();
}
public void countMe() {
for (int i = 0; i < 10; i++) {
try {
sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("The value of i is: " + i + ", thread no: " + threadNo);
}
}
}
public class ThreadingDemo {
public static void main(String[] args) {
MyCounter counter1 = new MyCounter(1);
MyCounter counter2 = new MyCounter(2);
long startTime = System.currentTimeMillis();
/*
* counter1.countMe();
*
* now instead of calling countMe() method directly, we call the run() method
* from Thread class.
*
* run() method should be called by the JVM instead of us calling it ourselves.
*/
// counter1.run(); // it should be called by JVM so rather we call the start()
counter1.start();
// counter2.countMe();
// counter2.run(); // calling start method rather than run
counter2.start();
try {
Thread.sleep(4100);
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("---------------------------------------------------------------");
System.out.println("\nTotal time taken: " + (endTime - startTime) + " seconds.");
}
}