Path: blob/main/src/lwjgl/java/paulscode/sound/SimpleThread.java
8644 views
package paulscode.sound;123/**4* The SimpleThread class is the template used to create all thread classes5* used by in the SoundSystem library. It provides methods for common actions6* like sleeping, killing, and checking liveness. NOTE: super.cleanup() must7* be called at the bottom of overriden cleanup() methods, and cleanup()8* must be called at the bottom of the run() method for all extended classes.9*<br><br>10*<b><i> SoundSystem License:</b></i><br><b><br>11* You are free to use this library for any purpose, commercial or otherwise.12* You may modify this library or source code, and distribute it any way you13* like, provided the following conditions are met:14*<br>15* 1) You may not falsely claim to be the author of this library or any16* unmodified portion of it.17*<br>18* 2) You may not copyright this library or a modified version of it and then19* sue me for copyright infringement.20*<br>21* 3) If you modify the source code, you must clearly document the changes22* made before redistributing the modified source code, so other users know23* it is not the original code.24*<br>25* 4) You are not required to give me credit for this library in any derived26* work, but if you do, you must also mention my website:27* http://www.paulscode.com28*<br>29* 5) I the author will not be responsible for any damages (physical,30* financial, or otherwise) caused by the use if this library or any part31* of it.32*<br>33* 6) I the author do not guarantee, warrant, or make any representations,34* either expressed or implied, regarding the use of this library or any35* part of it.36* <br><br>37* Author: Paul Lamb38* <br>39* http://www.paulscode.com40* </b>41*/42public class SimpleThread extends Thread43{44/**45* Used to return a current value from one of the synchronized46* boolean-interface methods.47*/48private static final boolean GET = false;4950/**51* Used to set the value in one of the synchronized boolean-interface methods.52*/53private static final boolean SET = true;5455/**56* Used when a parameter for one of the synchronized boolean-interface methods57* is not aplicable.58*/59private static final boolean XXX = false;6061/**62* True when thread is running.63*/64private boolean alive = true;6566/**67* True when thread should end.68*/69private boolean kill = false;7071/**72* Removes all references to instantiated objects, and changes the thread's73* state to "not alive". Method alive() returns false when this method has74* completed. NOTE: super.cleanup() must be called at the bottom of overriden75* cleanup() methods, and cleanup() must be called at the bottom of the run()76* method for all extended classes.77*/78protected void cleanup()79{80kill( SET, true ); // tread needs to shut down81alive( SET, false ); // thread has ended82}8384/**85* Executes the thread's main loop. NOTES: Extended classes should check86* method dying() often to know when the user wants the thread to shut down.87* Method cleanup() must be called at the bottom of the run() method for all88* extended classes.89*/90@Override91public void run()92{93/* How the run() method should be set up: */949596// Do your stuff here. Remember to check dying() often to know when97// the user wants the thread to shut down.9899// MUST call cleanup() at the bottom of Overridden run() method!!!!!100cleanup(); // clears memory and sets status to dead.101}102103/**104* Calls the rerun() method on a seperate thread, which calls run() when the105* previous thread finishes.106*/107public void restart()108{109new Thread()110{111@Override112public void run()113{114rerun();115}116}.start();117}118119/**120* Kills the previous thread, waits for it to die, then calls run().121*/122private void rerun()123{124kill( SET, true );125while( alive( GET, XXX ) )126{127snooze( 100 );128}129alive( SET, true );130kill( SET, false );131run();132}133134/**135* Returns false when the cleanup() method has finished. This method should be136* used to know when the thread has been safely shut down.137* @return True while the thread is alive.138*/139public boolean alive()140{141return alive( GET, XXX );142}143144/**145* Causes method dying() to return true, letting the thread know it needs to146* shut down.147*/148public void kill()149{150kill( SET, true );151}152153/**154* Returns true when the thread is supposed to shut down.155* @return True if the thread should die.156*/157protected boolean dying()158{159return kill( GET, XXX );160}161162/**163* Sets or returns the value of boolean 'alive'.164* @param action GET or SET.165* @param value New value if action == SET, or XXX if action == GET.166* @return True while the thread is alive.167*/168private synchronized boolean alive( boolean action, boolean value )169{170if( action == SET )171alive = value;172return alive;173}174175/**176* Sets or returns the value of boolean 'kill'.177* @param action GET or SET.178* @param value New value if action == SET, or XXX if action == GET.179* @return True if the thread should die.180*/181private synchronized boolean kill( boolean action, boolean value )182{183if( action == SET )184kill = value;185return kill;186}187188/**189* Sleeps for the specified number of milliseconds.190*/191protected void snooze( long milliseconds )192{193try194{195Thread.sleep( milliseconds );196}197catch( InterruptedException e ){}198}199}200201202