-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadMethodTest.java
More file actions
77 lines (68 loc) · 2.22 KB
/
Copy pathThreadMethodTest.java
File metadata and controls
77 lines (68 loc) · 2.22 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
package ThreadTest.exers;
/* 1. currentThread()
* 2. run()
* 3. start()
* 4. getName()
* 5. setName()
* 6. yield() ---> release current thread cpu memory
* 7. join() --> The join method allows one thread to wait for the completion of another.
* If t is a Thread object whose thread is currently executing
* 8. sleep(time: ms) : causes the current thread to suspend execution for a specified period
* 9. isAlive()
* 10. stop: deprecated
*
*11. setPriority( Thread.MAX_PRIORITY-> 10
* Thread.NORM_PRIORITY -> 5
* Thread.MIN_PRIORITY -> 1 )
* High priority doesn't mean it will be executed totally firstly, just in high likely like this, but not absolutely is.
* */
public class ThreadMethodTest {
public static void main(String[] args) {
/* way 2: use constructor*/
ThreadRandom1 t1 = new ThreadRandom1("线程1");
/* way1: use setName */
// t1.setName("线程一");
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
/* set name for main thread*/
Thread.currentThread().setName("主线程");
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
for (int i = 0; i < 100 ; i++) {
if (i%2 != 0){
System.out.println(Thread.currentThread().getName() + ": " + i);
}
// if (i == 33){
// try {
// t1.join(); // pull thread t1 in;
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
System.out.println(t1.isAlive());
}
}
class ThreadRandom1 extends Thread{
public ThreadRandom1(String name) {
super(name);
}
@Override
public void run() {
//
for (int i = 0; i < 100 ; i++) {
if (i%2==0){
System.out.println(Thread.currentThread().getName() + ": " + i);
}
// if(i % 10 == 0){
// yield();
// }
// if(i == 44){
// try {
// sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
}
}