-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78_Threads
More file actions
116 lines (90 loc) · 3.43 KB
/
78_Threads
File metadata and controls
116 lines (90 loc) · 3.43 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
/*
thread = A thread of execution in a program(kind of like a virtual CPU
The JVM allows an application to have multiple thread running concurrently
Each thread can execute parts of your code in parallel with the main thread
Each thread has a priority.
Threads with higher priority are executed in preference compared to threads with a lower priority
The Java Virtual Machine continues to execute threads until either of the following occurs
1. The exit method of class Runtime has been called
2. All user threads have died
When a JVM starts up, there is a thread which calls the main method
This thread is called "main"
Daemon thread is a low priority thread that runs in background to perform tasks such as garbage collection
JVM terminates itself when all user threads (non-daemon threads) finish their execution
-daemon has low priority
*/
<Main.java>
public class Main{
public static void main(String[] args) throws InterruptedException {
System.out.println(Thread.activeCount());
//>>1 [ which is main thread]
System.out.println(Thread.currentThread().getName());
//>>main
Thread.currentThread().setName("MAINNN");
System.out.println(Thread.currentThread().getName());
//>>MAINNNN
System.out.println(Thread.currentThread().getPriority());
//>>5 [Number of priority of main thread]
Thread.currentThread().setPriority(10);//10 highest, 1 lowest
System.out.println(Thread.currentThread().getPriority());
//>>10
System.out.println(Thread.currentThread().isAlive());
//>>true
//counter countdown by seconds
for(int i = 3; i>0; i--){
System.out.println(i);
Thread.sleep(1000); //Time set is in millisecond
/*>>
3
2
1
-Each appear in time interval of 1s
*/
}
System.out.println("You are done!");
// >>You are done! when all the {} done run
MyThread thread2 = new MyThread();
System.out.println(thread2.isAlive());
//>>false
thread2.start();
System.out.println(thread2.isAlive());
//>>true
// This thread is running
thread2.run();
System.out.println(thread2.isAlive());
//>>This thread is running
// false
System.out.println(thread2.getName());
//>>Thread-0
thread2.setName("2nd thread");
System.out.println(thread2.getName());
//>>2nd thread
System.out.println(thread2.getPriority());
//>>5 [default]
thread2.setPriority(1);
System.out.println(thread2.getPriority());
//>>1
//Check to see how many thread active
System.out.println(Thread.activeCount());
//>>2 [after thread2.start()]
System.out.println(thread2.isDaemon());
//>>false
// This is a user thread that is running
thread2.setDaemon(true);
System.out.println(thread2.isDaemon());
//>>true
// This is a daemon thread that is running
}
}
<MyThread.java>
public class MyThread extends Thread{
@Override
public void run(){
if(this.isDaemon()){
System.out.println("This is a daemon thread that is running");
}
else{
System.out.println("This is a user thread that is running");
}
}
}