// Fig. 15.3: ThreadTester.java
// Show multiple threads printing at different intervals.

public class ThreadTester {
public static void main( String args[] )
{
PrintThread thread1, thread2, thread3, thread4;

thread1 = new PrintThread( "thread1" );
thread2 = new PrintThread( "thread2" );
thread3 = new PrintThread( "thread3" );
thread4 = new PrintThread( "thread4" );

System.err.println( "\nStarting threads" );

thread1.start();
thread2.start();
thread3.start();
thread4.start();

System.err.println( "Threads started\n" );
}
}

class PrintThread extends Thread {
private int sleepTime;

// PrintThread constructor assigns name to thread
// by calling Thread constructor
public PrintThread( String name )
{
super( name );

// sleep between 0 and 5 seconds
sleepTime = (int) ( Math.random() * 5000 );

System.err.println( "Name: " + getName() +
"; sleep: " + sleepTime );
}

// execute the thread
public void run()
{
// put thread to sleep for a random interval
try {
System.err.println( getName() + " going to sleep" );
Thread.sleep( sleepTime );
}
catch ( InterruptedException exception ) {
System.err.println( exception.toString() );
}

// print thread name
System.err.println( getName() + " done sleeping" );
}
}

/ Fig. 15.4: SharedCell.java
// Show multiple threads modifying shared object.


public class SharedCell {
public static void main( String args[] )
{
HoldIntegerUnsynchronized h =
new HoldIntegerUnsynchronized();
ProduceInteger p = new ProduceInteger( h );
ConsumeInteger c = new ConsumeInteger( h );

p.start();
c.start();
}
}


// Fig. 15.4: ProduceInteger.java
// Definition of threaded class ProduceInteger


public class ProduceInteger extends Thread {
private HoldIntegerUnsynchronized pHold;

public ProduceInteger( HoldIntegerUnsynchronized h )
{
super( "ProduceInteger" );
pHold = h;
}

public void run()
{
for ( int count = 1; count <= 10; count++ ) {
// sleep for a random interval
try {
Thread.sleep( (int) ( Math.random() * 3000 ) );
}
catch( InterruptedException e ) {
System.err.println( e.toString() );
}

pHold.setSharedInt( count );
}

System.err.println( getName() +
" finished producing values" +
"\nTerminating " + getName() );
}
}

// Fig. 15.4: HoldIntegerUnsynchronized.java
// Definition of class HoldIntegerUnsynchronized


public class HoldIntegerUnsynchronized {
private int sharedInt = -1;

public void setSharedInt( int val )
{
System.err.println( Thread.currentThread().getName() +
" setting sharedInt to " + val );
sharedInt = val;
}

public int getSharedInt()
{
System.err.println( Thread.currentThread().getName() +
" retrieving sharedInt value " + sharedInt );
return sharedInt;
}
}


// Fig. 15.4: ConsumeInteger.java
// Definition of threaded class ConsumeInteger


public class ConsumeInteger extends Thread {
private HoldIntegerUnsynchronized cHold;

public ConsumeInteger( HoldIntegerUnsynchronized h )
{
super( "ConsumeInteger" );
cHold = h;
}

public void run()
{
int val, sum = 0;

do {
// sleep for a random interval
try {
Thread.sleep( (int) ( Math.random() * 3000 ) );
}
catch( InterruptedException e ) {
System.err.println( e.toString() );
}

val = cHold.getSharedInt();
sum += val;
} while ( val != 10 );

System.err.println(
getName() + " retrieved values totaling: " + sum +
"\nTerminating " + getName() );
}
}

// Fig. 15.7: RandomCharacters.java
// Demonstrating the Runnableinterface


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RandomCharacters extends JApplet
implements Runnable,
ActionListener {
private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private JLabel outputs[];
private JCheckBox checkboxes[];
private final static int SIZE = 3;

private Thread threads[];
private boolean suspended[];

public void init()
{
outputs = new JLabel[ SIZE ];
checkboxes = new JCheckBox[ SIZE ];
threads = new Thread[ SIZE ];
suspended = new boolean[ SIZE ];

Container c = getContentPane();
c.setLayout( new GridLayout( SIZE, 2, 5, 5 ) );

for ( int i = 0; i < SIZE; i++ ) {
outputs[ i ] = new JLabel();
outputs[ i ].setBackground( Color.green );
outputs[ i ].setOpaque( true );
c.add( outputs[ i ] );

checkboxes[ i ] = new JCheckBox( "Suspended" );
checkboxes[ i ].addActionListener( this );
c.add( checkboxes[ i ] );
}
}

public void start()
{
// create threads and start every time start is called
for ( int i = 0; i < threads.length; i++ ) {
threads[ i ] =
new Thread( this, "Thread " + (i + 1) );
threads[ i ].start();
}
}

public void run()
{
Thread currentThread = Thread.currentThread();
int index = getIndex( currentThread );
char displayChar;

while ( threads[ index ] == currentThread ) {
// sleep from 0 to 1 second
try {
Thread.sleep( (int) ( Math.random() * 1000 ) );

synchronized( this ) {
while ( suspended[ index ] &&
threads[ index ] == currentThread )
wait();
}
}
catch ( InterruptedException e ) {
System.err.println( "sleep interrupted" );
}

displayChar = alphabet.charAt(
(int) ( Math.random() * 26 ) );
outputs[ index ].setText( currentThread.getName() +
": " + displayChar );
}

System.err.println(
currentThread.getName() + " terminating" );
}

private int getIndex( Thread current )
{
for ( int i = 0; i < threads.length; i++ )
if ( current == threads[ i ] )
return i;

return -1;
}

public synchronized void stop()
{
// stop threads every time stop is called
// as the user browses another Web page
for ( int i = 0; i < threads.length; i++ )
threads[ i ] = null;

notifyAll();
}

public synchronized void actionPerformed( ActionEvent e )
{
for ( int i = 0; i < checkboxes.length; i++ ) {
if ( e.getSource() == checkboxes[ i ] ) {
suspended[ i ] = !suspended[ i ];

outputs[ i ].setBackground(
!suspended[ i ] ? Color.green : Color.red );

if ( !suspended[ i ] )
notify();

return;
}
}
}
}