-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiThread.java
More file actions
82 lines (78 loc) · 2.88 KB
/
MultiThread.java
File metadata and controls
82 lines (78 loc) · 2.88 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
package multiThreading;
public class MultiThread {
public static void main(String[] args) {
// create a thread using the thread consturctor
Thread t1 = new Thread();
Thread t2 = Thread.currentThread();
System.out.println(t2);
ThreadCreateClass tcc1 = new ThreadCreateClass();
ThreadCreateClass tcc2 = new ThreadCreateClass();
// System.out.println(t.getName());
// set a name to the thread using the setName() method.
t1.setName("HelloWorld");
// get the name of the thread using the getName() method.
System.out.println(t1.getName());
// get the priority of a thread using the thread priority method
System.out.println(t1.getPriority());
// check if a thread is alive or not using isAlive() mehtod.
System.out.println(t1.isAlive());
// Starts the tcc2 Thread using start() method.
tcc1.start();
// thread tcc1 calls the join() method which makes the tcc2 thread wait
// till tcc1 finish its execution completely.
// try{
// tcc1.join();
// }catch(Exception e){
// e.printStackTrace();
// }
// Starts the tcc2 Thread using start() method.
tcc2.start();
// the number of active threads in the current thread's threadGroup and
// subgroup can be found out using activeCount() method.
System.out.println(ThreadCreateClass.activeCount());
// prints the stack trace of the thread using dumpStack() method. it throws an exception always.
// try {
// Thread.dumpStack();
// }catch(Exception e){
// e.printStackTrace();
// }
System.out.println(t1.getId());
System.out.println(tcc2.getState());
System.out.println(tcc2.getThreadGroup());
tcc2.interrupt();
System.out.println(Thread.interrupted());
System.out.println(tcc1.isDaemon());
System.out.println(tcc1.isInterrupted());
// tcc1.setDaemon(false);
System.out.println(tcc1.isDaemon());
tcc1.setPriority(7);
System.out.println(tcc1.getPriority());
// tcc1.yield();
// shutdown hook demonstration
Runtime rt = Runtime.getRuntime();
ShHook sh = new ShHook();
rt.addShutdownHook(sh);
Runtime.getRuntime().addShutdownHook(new ShHook());
}
}
class ThreadCreateClass extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.print(i + " ");
// makes the thread sleep for 0.1s (100ms) in each iteration.
// try {
// Thread.sleep(100);
// }catch (Exception w){
// w.printStackTrace();
// }
}
}
}
class ShHook extends Thread{
@Override
public void run(){
System.out.println("shutdown hook");
System.out.println("bye");
}
}