The class T2
implements Runnable
class.
More...
Public Member Functions | |
T2 (JPWorld parent, float sleepIntervalSec) | |
Creates a new instance of T1. | |
void | startThread () |
Starts the thread. | |
void | stopThread () |
Stops the thread. | |
void | disableThread () |
Disables (pauses) the thread. | |
void | enableThread () |
Enables the thread after paused. | |
void | interruptibleSleep (long millis) |
Interruptible sleep (replacement for Thread.sleep ). | |
void | run () |
Prints thread's own signature and creates a pair of particles in belonging JPWorld 's context every sleepItervalMillis . | |
Protected Attributes | |
JPWorld | parent |
Parent where println() messages are directed to. | |
Private Attributes | |
String | signature = "T2: Thread 2" |
volatile boolean | running = false |
Indicates if thread is (or should be) running. | |
volatile boolean | active = false |
Indicates that thread is active and not paused. | |
volatile Thread | mainThread = null |
Instance of the main thread. | |
int | sleepIntervalMillis |
Sleep interval for thread in millis. |
The class T2
implements Runnable
class.
Instance of the T2
prints out its signature until it gets stopped. It also creates a charged particle pair in associated JPWorld
context.
Definition at line 10 of file T2.java.
T2.T2 | ( | JPWorld | parent, |
float | sleepIntervalSec | ||
) |
Creates a new instance of T1.
parent | owner of the thread |
sleepIntervalSec | sleep interval in seconds |
Definition at line 45 of file T2.java.
References parent, and sleepIntervalMillis.
{ this.parent = parent; this.sleepIntervalMillis = (int)( sleepIntervalSec * 1000f ); }
void T2.disableThread | ( | ) |
void T2.enableThread | ( | ) |
void T2.interruptibleSleep | ( | long | millis ) |
Interruptible sleep (replacement for Thread.sleep
).
millis | - the length of time to sleep in milliseconds. |
Definition at line 111 of file T2.java.
References running.
Referenced by run().
{ synchronized( this ) { try { this.wait( millis ); } catch( InterruptedException ie ) { running = false; // signals thread to quit } } }
void T2.run | ( | ) |
Prints thread's own signature and creates a pair of particles in belonging JPWorld
's context every sleepItervalMillis
.
Definition at line 129 of file T2.java.
References active, JPWorld.createPairOfChargedParticles(), interruptibleSleep(), mainThread, parent, JPWorld.println(), running, signature, and sleepIntervalMillis.
{ while( running ) { while( running && active ) { parent.println( signature ); parent.createPairOfChargedParticles( 1, 10.0 + Math.random () * 5.0 ); interruptibleSleep( sleepIntervalMillis ); } while( running && ! active ) { interruptibleSleep( sleepIntervalMillis ); } } synchronized( this ) { running = false; active = false; mainThread = null; } }
void T2.startThread | ( | ) |
Starts the thread.
Definition at line 54 of file T2.java.
References active, mainThread, and running.
Referenced by T3.run().
{ synchronized( this ) { if ( mainThread != null ) { return; // Allow only single thread per instance } running = true; active = true; mainThread = new Thread( this ); mainThread.start (); } }
void T2.stopThread | ( | ) |
volatile boolean T2.active = false [private] |
Indicates that thread is active and not paused.
Definition at line 22 of file T2.java.
Referenced by disableThread(), enableThread(), run(), startThread(), and stopThread().
volatile Thread T2.mainThread = null [private] |
Instance of the main thread.
Definition at line 27 of file T2.java.
Referenced by run(), and startThread().
volatile boolean T2.running = false [private] |
Indicates if thread is (or should be) running.
Definition at line 17 of file T2.java.
Referenced by interruptibleSleep(), run(), startThread(), and stopThread().
String T2.signature = "T2: Thread 2" [private] |
int T2.sleepIntervalMillis [private] |