Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
285 lines (210 loc) · 7.31 KB

File metadata and controls

285 lines (210 loc) · 7.31 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Main threads cheat.
*/
public class Main {
static int i = 0;
/**
* # Runnable
*
* The main way to create a thread is to make a class that implements Runnable.
*/
static class RunnableTest implements Runnable {
private int id;
public RunnableTest(int id) {
this.id = id;
}
/**
* This is what will be run when the thread is started.
*/
public void run() {
System.out.print(id + " ");
// Exits all threads of the program. TODO example program.
//System.exit(0);
}
}
public static void main(String[] args) throws InterruptedException {
/*
# Daemon
# getDaemon
# setDaemon
There are 2 types of thread: daemon and user.
When there are no user threads left,
the JVM kills all daemon threads and ends the program.
*/
{
// By default, the main thread is user.
assert !Thread.currentThread().isDaemon();
// You can only setDaemon on a thread before you start it
boolean throwed = false;
try {
Thread.currentThread().setDaemon(true);
} catch(IllegalThreadStateException e) {
throwed = true;
}
assert throwed;
}
/*
# Priority
# getPriority
Each thread has an associated priority between 1 and 10.
TODO what is the exact effect?
*/
{
/*
# NORM_PRIORITY
Default priority
*/
{
assert Thread.currentThread().getPriority() == Thread.NORM_PRIORITY;
// Unlike in Linux, you can increase your own priority by default.
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
// IllegalArgumentException
//Thread.currentThread().setPriority(Thread.MAX_PRIORITY + 1);
}
}
/*
# Name
# getName
# setName
Each thread has a name.
Giving meaningful names to your threads can greatly help during debugging,
as the name shows on stack traces and on the Eclipse debugger.
# Get thread by name
http://stackoverflow.com/questions/15370120/get-thread-by-name
No direct way to do it.
http://stackoverflow.com/questions/15370120/get-thread-by-name
*/
{
/*
The initial thread is called `main`. TODO specified?
Further threads are called TODO
*/
assert Thread.currentThread().getName().equals("main");
// By default, new threads are user.
// TODO example
}
/*
# Id
# getId
Each thread has an ID.
It is generated at creation time, and cannot be modified.
*/
{
System.out.println("getId() = " + Thread.currentThread().getId());
}
/*
# state
# getState
Threads can be in the following states:
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
State transition diagram:
http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html
*/
{
// TODO examples
}
/*
# start
Start running a given Runnable as a new thread.
# Anonymous runnable
For short examples, it is common to use anonymous inner class runners,
but you cannot pass non-final parameters to them:
- nested classes cannot use non-final variables from the outside
- anonymous classes cannot have constructors
*/
{
}
/*
# ThreadGroup
# getThreadGroup
TODO understand
Defines a tree of threads:
http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html
*/
/*
# Get all threads
# List all threads
http://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java
*/
{
Thread[] threads = new Thread[100];
/*
# start
Start running the runnable run method on a new thread.
*/
for ( int i = 0; i < threads.length; i++ ) {
threads[i] = new Thread(new RunnableTest(i));
threads[i].start();
}
/*
# join
Wait for thread to finish running.
*/
for ( int i = 0; i < threads.length; i++ ) {
threads[i].join();
}
System.out.println();
/*
# yield
Suggest OS that he take another thread to run.
OS may return to current thread at any time.
Use this when you have to wait for a contidion which you don't know
how much time will take to happen.
*/
{
Thread.yield();
}
}
/*
# interrupt
Tells a thread to stop that it is doing.
The only effect of this is to set the isInterrupted flag, which can be querried with
`Thread.currentThread().isInterrupted()`.
Threads that take long actins can check from time to time if they have been interrupted,
and if so, cleanup and end operation.
# InterruptedException
Thrown when a thread is sleeping or waiting and another thread calls interrupt on it.
Rationale: a thread who is sleeping or waiting may take too much time to check isInterrupted,
so it gets notified with an exception.
When this is raised on a thread, isInterrupted flag is cleared!
For this reason, you should not simply ignore an InterruptedException.
If you don't know what to do with it, do `Thread.currentThread().interrupt();`,
which plays nicelly and does what the other thread kindly asked you to do.
<http://michaelscharf.blogspot.fr/2006/09/dont-swallow-interruptedexception-call.html>
*/
{
System.out.format(
"isInterrupted = %b%n",
Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
assert Thread.currentThread().isInterrupted() == true;
}
/*
# wait
# nofity
# nofityAll
Sleep until another thread calls `notify` on us.
TODO get working
*/
{
/*
i = 0;
Thread thread = new Thread( new Runnable(){
public void run() {
try {
assert i == 0;
this.wait();
assert i == 1;
} catch ( InterruptedException e ) {
Thread.currentThread().interrupt();
}
}
});
thread.start();
i = 1;
thread.notify();
thread.join();
*/
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.