-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path79_Multithreading
More file actions
156 lines (143 loc) · 3.34 KB
/
79_Multithreading
File metadata and controls
156 lines (143 loc) · 3.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* multithreading = Process of executing multiple threads simultaneously
Helps maximum utilization of CPU
Threads are independent, they don't affect the execution of
An exception in one thread will not interrupt other threads
useful for serving multiple cilents, multiplayer games, or other mutually independent tasks
*/
<Main.java>
public class Main{
public static void main(String[] args){
MyThread thread1 = new MyThread(); //1st method via MyThread.java (subclass)
//2nd method via MyRunnable.java (class implements runnable runnable interface, pass instance as an argument to Thread arguments)
MyRunnable runnable1 = new MyRunnable();
Thread thread2 = new Thread(runnable1);
thread1.start();
thread2.start();
}
}
<MyThread.java>
public class MyThread extends Thread{
@Override
public void run(){
for(int i = 10; i>=0; i--){
System.out.println("Thread #1: " + i);
try {
Thread.sleep(1000); //To set time interval between each loop
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread #1 is finished :)");
}
}
<MyRunnable.java>
public class MyRunnable implements Runnable{
@Override
public void run() {
for(int i = 0; i<10; i++){
System.out.println("Thread #2: " +i);
try {
Thread.sleep(1000); //To set time interval between each loop
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread #2 is finished :)");
}
}
>>
Thread #2: 0
Thread #1: 10
Thread #1: 9
Thread #2: 1
Thread #1: 8
Thread #2: 2
Thread #1: 7
Thread #2: 3
Thread #2: 4
Thread #1: 6
Thread #1: 5
Thread #2: 5
Thread #1: 4
Thread #2: 6
Thread #2: 7
Thread #1: 3
Thread #1: 2
Thread #2: 8
Thread #2: 9
Thread #1: 1
Thread #2 is finished :)
Thread #1: 0
Thread #1 is finished :)
<Main.java>
...
thread1.start();
thread1.join(); //wait thread1 finish, only start thread2
thread2.start();
...
>>
Thread #1: 10
Thread #1: 9
Thread #1: 8
Thread #1: 7
Thread #1: 6
Thread #1: 5
Thread #1: 4
Thread #1: 3
Thread #1: 2
Thread #1: 1
Thread #1: 0
Thread #1 is finished :)
Thread #2: 0
Thread #2: 1
Thread #2: 2
Thread #2: 3
Thread #2: 4
Thread #2: 5
Thread #2: 6
Thread #2: 7
Thread #2: 8
Thread #2: 9
Thread #2 is finished :)
<Main.java>
...
thread1.start();
thread1.join(3000);//wait thread1 started 3s, only start thread2
thread2.start();
...
>>
Thread #1: 10
Thread #1: 9
Thread #1: 8
Thread #2: 0
Thread #1: 7
Thread #2: 1
Thread #1: 6
Thread #2: 2
Thread #1: 5
Thread #2: 3
Thread #1: 4
Thread #2: 4
Thread #1: 3
Thread #2: 5
Thread #1: 2
Thread #2: 6
Thread #1: 1
Thread #2: 7
Thread #1: 0
Thread #2: 8
Thread #1 is finished :)
Thread #2: 9
Thread #2 is finished :)
//calling thread(ex.main) wait until the specified thread dies or for x milliseconds
-----
/*
daemon thread = background thread (non-user)
JVM will not wait daemon execute
if have 1 user thread
JVM wait to exit until all user thread finish
*/
...thread1.setDaemon(true);
thread2.setDaemon(true);...
-Terminate if main have exception
\