Java

TOPIC 29 – MULTI-THREADING

 

LESSON WORK

 

 

TEACHER DEMO #1A

·                         Create a SimpleThread class that extend Thread. 

·                         Implement the run() method that gets automatically called by the start() method.

·                         Show that a Thread object’s run method is called by start().

 

public class SimpleThread extends Thread

{

     public String name;

    

     public SimpleThread(String tname)

     {

           name = tname;

     }

    

     @Override

     public void run()

     {

          System.out.println(name + " is running.");

     }

}

public class Tester

{

     public static void main(String[] args) throws InterruptedException

     {

          SimpleThread t1 = new SimpleThread("Jimmy");

           t1.start();          

     }

}

 

TEACHER DEMO #1B

-Add infinite loops in both classes so that both threads are in infinite loops.  Without multi-threading, we would only see one loop forever, but we actually see the computer bouncing back and forth between the two infinite loops.  Multi-threading!

public class SimpleThread extends Thread

{

     public String name;

    

     public SimpleThread(String tname)

     {

           name = tname;

     }

    

     @Override

     public void run()

     {

           while(true)

           {

                System.out.println(name + " is running.");

           }

     }

}

 

public class Tester

{

     public static void main(String[] args)

     {

           SimpleThread t1 = new SimpleThread("Jimmy");

           t1.start();

          

           while(true)

           {

                System.out.println("MAIN is running");

           }

     }

}

 

DEMO #1C

Add sleeps to both the main thread and the SimpleThread.  The run method requires a try/catch.  The main could just throw the exception.

 

public class SimpleThread extends Thread

{

     public String name;

    

     public SimpleThread(String tname)

     {

           name = tname;

     }

    

     @Override

     public void run()

     {

           while(true)

           {

                System.out.println(name + " is running.");

                try

                {

                      Thread.sleep(500);

                }

                catch (InterruptedException e)

                {

                      e.printStackTrace();

                }

           }

     }

}

public class Tester

{

     public static void main(String[] args) throws InterruptedException

     {

           SimpleThread t1 = new SimpleThread("Jimmy");

           t1.start();

          

           while(true)

           {

                System.out.println("MAIN is running");

                Thread.sleep(5000);

           }

     }

}

 

DEMO #2 – ALTERNATING THREADS

Create two threads that alternate doing their jobs.  Below is a solution though it maybe not be perfect as it is possible for a thread to be interrupted right after the if statement in run().

 

public class AlternatingThread extends Thread

{

     public String name;

     public boolean jobStarted;

     public boolean jobDone;

     public AlternatingThread other;

 

     public AlternatingThread(String tname)

     {

           name = tname;

           jobStarted = false;

           jobDone = true;

           other = null;

     }

    

     public void setOther(AlternatingThread other)

     {

           this.other = other;

     }

    

     @Override

     public void run()

     {

           while(true)

           {

                if (other.jobStarted == false)

                {

                      jobStarted = true;

                      jobDone = false;

                      System.out.println(name + " is jobbing.");

                      try

                      {

                           Thread.sleep(1000);

                      } catch (InterruptedException e)

                      {

                           e.printStackTrace();

                      }

                      jobStarted = false;

                      jobDone = true;

                      System.out.println(name + " is done.");

                }

 

                //delay to allow other thread to get a chance

                try {

                      Thread.sleep(100);

                } catch (InterruptedException e)

                {

                      e.printStackTrace();

                }

               

           }

     }

    

}

 

public class AlternatingTester

{

     public static void main(String[] args)

     {

           AlternatingThread t01 = new AlternatingThread("Harold");

           AlternatingThread t02 = new AlternatingThread("Carol");

           t01.setOther(t02);

           t02.setOther(t01);

           t01.start();

           t02.start();

     }

 

}


QUESTION #1 (TIME PERMITTING)

 

Create a CountdownThread class that will countdown from 1000 down to 0 before outputting Blast off!  In a main method, create several CountdownThreads and have them race to their blast off.  Include adequate Thread.sleep calls to allow for various winners to happen.


QUESTION #2 (TIME PERMITTING)

We will create a two-threaded application that will have one thread continuously getting input from the user while the other thread draws graphics from that input.  This requires resource sharing amongst the threads and will allow us to consider the need for synchronization.