Thread Synchronzation
import java.util.*;
import java.io.*;
class TestThreadSynch
{
public static void main(String [] args )
{
TestThreadSynch t = new TestThreadSynch();
t.doIt();
}
public void doIt()
{
//All our prints to standard out(screen) will be done through this object so that we can force
// synchronization problems
OurScreenPrinter osp = new OurScreenPrinter();
MyThread t1 = new MyThread(10,10,osp, "Thread 1");
t1.start();
if (t1.isAlive()) System.out.println(t1.filler + "Thread 1 is alive");
else System.out.println(t1.filler +"Thread is not Alive");
MyThread t2 = new MyThread(10,300,osp, "Thread 2");
t2.start();
MyThread t3 = new MyThread(10,600,osp, "Thread 3");
t3.start();
try { t1.join(); } catch (InterruptedException e) { System.out.println(e.getMessage()); }
System.out.println( t1.filler + "Thread t1 is all done");
if (t1.isAlive()) System.out.println(t1.filler + "Thread 1 is alive");
else System.out.println(t1.filler +"Thread is not Alive");
try { t2.join(); } catch (InterruptedException e) { System.out.println(e.getMessage()); }
System.out.println(t2.filler + "Thread t2 is all done");
System.out.println(t3.filler + "Suspending t3 for 2 seconds");
t3.suspend();
try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.println(e.getMessage()); };
t3.resume();
try { t3.join(); } catch (InterruptedException e) { System.out.println(e.getMessage()); }
System.out.println(t3.filler + "Thread t3 is all done");
}
}
******************************************************************************************************
import java.io.*;
class MyThread extends Thread
{
static String spacerString ="";
public String filler;
int sackTime;
int iterations;
OurScreenPrinter osp;
public MyThread(int iterationsIn, int sackTimeIn, OurScreenPrinter ospIn, String ThreadNameIn)
{
sackTime = sackTimeIn;
osp = ospIn; //This is the Object that will need to be Synched
iterations = iterationsIn;
this.setName(ThreadNameIn);
filler= spacerString;
spacerString = spacerString + " ";
}
public void run()
{
for (int k=0; k < iterations; k++)
{
try { Thread.sleep(sackTime); } catch (InterruptedException e) { System.out.println(e.getMessage()); };
String outputString = filler + Thread.currentThread().getName() + " Iteration = " + Integer.toString(k);
osp.print(outputString); //This is the object/method that will need to be synched
}
}
}
******************************************************************************************************
class OurScreenPrinter
{
StringBuffer holdString = new StringBuffer("");
//public void print(String stringToPrintIn)
public synchronized void print(String stringToPrintIn)
{
//Set the String to Print.
holdString.setLength(0);
holdString.append(stringToPrintIn);
//Sleep for 1/10 second. The holdString buffer is vulnerable to be changed and changed again by multiple threads
//during this time.
try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e.getMessage()); }
//Now Print it
System.out.println(holdString.toString());
}
}
Output:
java TestThreadSynch
Thread 1 is alive
Thread 1 Iteration = 0
Thread 1 Iteration = 1
Thread 1 Iteration = 2
Thread 2 Iteration = 0
Thread 1 Iteration = 3
Thread 3 Iteration = 0
Thread 1 Iteration = 4
Thread 2 Iteration = 1
Thread 1 Iteration = 5
Thread 1 Iteration = 6
Thread 1 Iteration = 7
Thread 2 Iteration = 2
Thread 3 Iteration = 1
Thread 1 Iteration = 8
Thread 1 Iteration = 9
Thread t1 is all done
Thread is not Alive
Thread 2 Iteration = 3
Thread 3 Iteration = 2
Thread 2 Iteration = 4
Thread 2 Iteration = 5
Thread 3 Iteration = 3
Thread 2 Iteration = 6
Thread 2 Iteration = 7
Thread 3 Iteration = 4
Thread 2 Iteration = 8
Thread 3 Iteration = 5
Thread 2 Iteration = 9
Thread t2 is all done
Suspending t3 for 2 seconds
Thread 3 Iteration = 6
Thread 3 Iteration = 7
Thread 3 Iteration = 8
Thread 3 Iteration = 9
Thread t3 is all done