Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/paulscode/sound/SimpleThread.java
8644 views
1
package paulscode.sound;
2
3
4
/**
5
* The SimpleThread class is the template used to create all thread classes
6
* used by in the SoundSystem library. It provides methods for common actions
7
* like sleeping, killing, and checking liveness. NOTE: super.cleanup() must
8
* be called at the bottom of overriden cleanup() methods, and cleanup()
9
* must be called at the bottom of the run() method for all extended classes.
10
*<br><br>
11
*<b><i> SoundSystem License:</b></i><br><b><br>
12
* You are free to use this library for any purpose, commercial or otherwise.
13
* You may modify this library or source code, and distribute it any way you
14
* like, provided the following conditions are met:
15
*<br>
16
* 1) You may not falsely claim to be the author of this library or any
17
* unmodified portion of it.
18
*<br>
19
* 2) You may not copyright this library or a modified version of it and then
20
* sue me for copyright infringement.
21
*<br>
22
* 3) If you modify the source code, you must clearly document the changes
23
* made before redistributing the modified source code, so other users know
24
* it is not the original code.
25
*<br>
26
* 4) You are not required to give me credit for this library in any derived
27
* work, but if you do, you must also mention my website:
28
* http://www.paulscode.com
29
*<br>
30
* 5) I the author will not be responsible for any damages (physical,
31
* financial, or otherwise) caused by the use if this library or any part
32
* of it.
33
*<br>
34
* 6) I the author do not guarantee, warrant, or make any representations,
35
* either expressed or implied, regarding the use of this library or any
36
* part of it.
37
* <br><br>
38
* Author: Paul Lamb
39
* <br>
40
* http://www.paulscode.com
41
* </b>
42
*/
43
public class SimpleThread extends Thread
44
{
45
/**
46
* Used to return a current value from one of the synchronized
47
* boolean-interface methods.
48
*/
49
private static final boolean GET = false;
50
51
/**
52
* Used to set the value in one of the synchronized boolean-interface methods.
53
*/
54
private static final boolean SET = true;
55
56
/**
57
* Used when a parameter for one of the synchronized boolean-interface methods
58
* is not aplicable.
59
*/
60
private static final boolean XXX = false;
61
62
/**
63
* True when thread is running.
64
*/
65
private boolean alive = true;
66
67
/**
68
* True when thread should end.
69
*/
70
private boolean kill = false;
71
72
/**
73
* Removes all references to instantiated objects, and changes the thread's
74
* state to "not alive". Method alive() returns false when this method has
75
* completed. NOTE: super.cleanup() must be called at the bottom of overriden
76
* cleanup() methods, and cleanup() must be called at the bottom of the run()
77
* method for all extended classes.
78
*/
79
protected void cleanup()
80
{
81
kill( SET, true ); // tread needs to shut down
82
alive( SET, false ); // thread has ended
83
}
84
85
/**
86
* Executes the thread's main loop. NOTES: Extended classes should check
87
* method dying() often to know when the user wants the thread to shut down.
88
* Method cleanup() must be called at the bottom of the run() method for all
89
* extended classes.
90
*/
91
@Override
92
public void run()
93
{
94
/* How the run() method should be set up: */
95
96
97
// Do your stuff here. Remember to check dying() often to know when
98
// the user wants the thread to shut down.
99
100
// MUST call cleanup() at the bottom of Overridden run() method!!!!!
101
cleanup(); // clears memory and sets status to dead.
102
}
103
104
/**
105
* Calls the rerun() method on a seperate thread, which calls run() when the
106
* previous thread finishes.
107
*/
108
public void restart()
109
{
110
new Thread()
111
{
112
@Override
113
public void run()
114
{
115
rerun();
116
}
117
}.start();
118
}
119
120
/**
121
* Kills the previous thread, waits for it to die, then calls run().
122
*/
123
private void rerun()
124
{
125
kill( SET, true );
126
while( alive( GET, XXX ) )
127
{
128
snooze( 100 );
129
}
130
alive( SET, true );
131
kill( SET, false );
132
run();
133
}
134
135
/**
136
* Returns false when the cleanup() method has finished. This method should be
137
* used to know when the thread has been safely shut down.
138
* @return True while the thread is alive.
139
*/
140
public boolean alive()
141
{
142
return alive( GET, XXX );
143
}
144
145
/**
146
* Causes method dying() to return true, letting the thread know it needs to
147
* shut down.
148
*/
149
public void kill()
150
{
151
kill( SET, true );
152
}
153
154
/**
155
* Returns true when the thread is supposed to shut down.
156
* @return True if the thread should die.
157
*/
158
protected boolean dying()
159
{
160
return kill( GET, XXX );
161
}
162
163
/**
164
* Sets or returns the value of boolean 'alive'.
165
* @param action GET or SET.
166
* @param value New value if action == SET, or XXX if action == GET.
167
* @return True while the thread is alive.
168
*/
169
private synchronized boolean alive( boolean action, boolean value )
170
{
171
if( action == SET )
172
alive = value;
173
return alive;
174
}
175
176
/**
177
* Sets or returns the value of boolean 'kill'.
178
* @param action GET or SET.
179
* @param value New value if action == SET, or XXX if action == GET.
180
* @return True if the thread should die.
181
*/
182
private synchronized boolean kill( boolean action, boolean value )
183
{
184
if( action == SET )
185
kill = value;
186
return kill;
187
}
188
189
/**
190
* Sleeps for the specified number of milliseconds.
191
*/
192
protected void snooze( long milliseconds )
193
{
194
try
195
{
196
Thread.sleep( milliseconds );
197
}
198
catch( InterruptedException e ){}
199
}
200
}
201
202