-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.java
More file actions
129 lines (102 loc) · 4.25 KB
/
Copy pathHelper.java
File metadata and controls
129 lines (102 loc) · 4.25 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
// Java program to demonstrate
// method calls of Thread class
package generic;
class Helper implements Runnable
{
public void run()
{
try
{
System.out.println("thread2 going to sleep for 5000");
Thread.sleep(5000);
} catch (InterruptedException e)
{
System.out.println("Thread2 interrupted");}
}
}
public class Test implements Runnable
{
public void run()
{
//thread run() method
}
public static void main(String[] args)
{
Test obj = new Test();
Helper obj2 = new Helper();
Thread thread1 = new Thread(obj);
Thread thread2 = new Thread(obj2);
// moving thread to runnable states
thread1.start();
thread2.start();
ClassLoader loader = thread1.getContextClassLoader();
Thread thread3 = new Thread(new Helper());
// getting number of active threads
System.out.println(Thread.activeCount());
thread1.checkAccess();
// fetching an instance of this thread
Thread t = Thread.currentThread();
System.out.println(t.getName());
System.out.println("Thread1 name: "+thread1.getName());
System.out.println("Thread1 ID: " + thread1.getId());
// fetching the priority and state of thread1
System.out.println("Priority of thread1 = " + thread1.getPriority());
System.out.println(thread1.getState());
thread2 = new Thread(obj2);
thread2.start();
thread2.interrupt();
System.out.println("Is thread2 interrupted? " + thread2.interrupted() );
System.out.println("Is thread2 alive? " + thread2.isAlive());
thread1 = new Thread(obj);
thread1.setDaemon(true);
System.out.println("Is thread1 a daemon thread? " + thread1.isDaemon());
System.out.println("Is thread1 interrupted? " + thread1.isInterrupted());
// waiting for thread2 to complete its execution
System.out.println("thread1 waiting for thread2 to join");
try
{
thread2.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// setting the name of thread1
thread1.setName("child thread xyz");
System.out.println("New name set for thread 1" + thread1.getName());
// setting the priority of thread1
thread1.setPriority(5);
thread2.yield();
// fetching the string representation of thread1
System.out.println(thread1.toString());
// getting list of active thread in current thread's group
Thread[] tarray = new Thread[3];
Thread.enumerate(tarray);
System.out.println("List of active threads:");
System.out.printf("[");
for(Thread thread : tarray)
{
System.out.println(thread);
}
System.out.printf("]\n");
System.out.println(Thread.getAllStackTraces());
ClassLoader classLoader = thread1.getContextClassLoader();
System.out.println(classLoader.toString());
System.out.println(thread1.getDefaultUncaughtExceptionHandler());
thread2.setUncaughtExceptionHandler(thread1.getDefaultUncaughtExceptionHandler());
thread1.setContextClassLoader(thread2.getContextClassLoader());
thread1.setDefaultUncaughtExceptionHandler(thread2.getUncaughtExceptionHandler());
thread1 = new Thread(obj);
StackTraceElement[] trace = thread1.getStackTrace();
System.out.println("Printing stack trace elements for thread1:");
for(StackTraceElement e : trace)
{
System.out.println(e);
}
ThreadGroup grp = thread1.getThreadGroup();
System.out.println("ThreadGroup to which thread1 belongs " +grp.toString());
System.out.println(thread1.getUncaughtExceptionHandler());
System.out.println("Does thread1 holds Lock? " + thread1.holdsLock(obj2));
Thread.dumpStack();
}
}