Public Member Functions | Protected Attributes | Private Attributes

T2 Class Reference

The class T2 implements Runnable class. More...

Collaboration diagram for T2:
Collaboration graph
[legend]

List of all members.

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.

Detailed Description

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.

Author:
Mikica B Kocic

Definition at line 10 of file T2.java.


Constructor & Destructor Documentation

T2.T2 ( JPWorld  parent,
float  sleepIntervalSec 
)

Creates a new instance of T1.

Parameters:
parentowner of the thread
sleepIntervalSecsleep interval in seconds

Definition at line 45 of file T2.java.

References parent, and sleepIntervalMillis.

    {
        this.parent = parent;
        this.sleepIntervalMillis = (int)( sleepIntervalSec * 1000f );
    }

Member Function Documentation

void T2.disableThread (  )

Disables (pauses) the thread.

Definition at line 85 of file T2.java.

References active.

Referenced by T3.run().

    {
        synchronized( this )
        {
            active = false;
            this.notifyAll ();
        }
    }
void T2.enableThread (  )

Enables the thread after paused.

Definition at line 97 of file T2.java.

References active.

Referenced by T3.run().

    {
        synchronized( this )
        {
            active = true;
            this.notifyAll ();
        }
    }
void T2.interruptibleSleep ( long  millis )

Interruptible sleep (replacement for Thread.sleep).

Parameters:
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 (  )

Stops the thread.

Definition at line 72 of file T2.java.

References active, and running.

Referenced by T3.run().

    {
        synchronized( this )
        {
            running = false;
            active  = false;
            this.notifyAll ();
        }
    }

Member Data Documentation

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().

JPWorld T2.parent [protected]

Parent where println() messages are directed to.

Definition at line 37 of file T2.java.

Referenced by run(), and T2().

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]

Definition at line 12 of file T2.java.

Referenced by run().

int T2.sleepIntervalMillis [private]

Sleep interval for thread in millis.

Definition at line 32 of file T2.java.

Referenced by run(), and T2().


The documentation for this class was generated from the following file: