Threads

 

 

A thread is a single sequential flow of control within a program.  People use threads to isolate tasks.  Threads have priorities.  For example, if you have one thread running at the same time as another and the first thread has a higher priority then the first process will be executed first.  All threads have a beginning, an execution sequence, and an end.

 

 

A browser is a perfect example of a multi-thread application.  You can scroll a page while it is still loading.  You can play sounds and animation concurrently. 

 

 

 

 

The run method gives a thread something to do.  Its code implements the threads running behavior (Runnable).  Basc support for run methods in Java are found in java.lang.thread class.  It provide the thread API and all the basic behavior for a thread.  These behaviors include starting, sleeping, running, yielding, and having a priority.

 

Threads are required in Java if you plan to design an applet that involves multi-tasking.  For example, if you need to have sound playing in the background while you are prompting the user for some input then you need a thread.  In addition, if you need the screen to refresh or to repaint part of the GUI then you will need to use threads. 

 

In this tutorial, I added simple thread code to the “Easter” lab to demonstrate how animation works in Java.  The resulting “EasterThread” applet code demonstrates how we can force the “Easter” lab to continue running:

 

 

 

 

 

 //McD's Advance Easter Lab

 

 

import java.awt.*;

import java.applet.*;

 

// had to include all applet libraries for sound to work

 

public class EasterThread extends Applet implements Runnable

           

            {

           

            private Image easteregg;

            private AudioClip chicken;

           

           

           

            int y;  // forced to make x,y global when 2 or more loops involved

            int x;

           

           

            Thread runner;

            int delay = 7000;

           

                       

            public void init()

            {

                        easteregg = getImage(getDocumentBase(),"egg1.gif");

                        chicken = getAudioClip(getDocumentBase(), "CHICKEN.AU");

    }

   

   

   

    //  ********************  Thread *****************************************

   

   

   

    public void start()

    {

        if (runner == null)

        {

            runner = new Thread (this);

            runner.start();

           

        }

    }

                         

           

           

             public void stop()

    {

        if (runner != null)

        {

            runner.stop();

            runner = null;

        }

    }

 

    public void run()

    {

        while(true)

        {

            try {runner.sleep(delay);}

            catch (Exception e) { }

 

            repaint();

            // this clears the canvas before printing

           

        }

    }

   

   

   

    // ************************* end of thread  *****************************

   

   

   

    public void paint (Graphics g)

    {

           

    for(y=0;y<200;++y)

            {

            g.drawImage(easteregg,1,y,this);

            // chicken.play();   - disabled chicken sound here because it is too slow in browser

           

                       

    }

   

   

    for(x=0;x<400;++x)

            {

            g.drawImage(easteregg,x,200,this);

            // chicken.play();

           

           

           

    }

   

    for(y=200;y>0;--y)

            {

            g.drawImage(easteregg,400,y,this);

            // chicken.play();

           

                       

    }

   

   

    for(x=400;x>0;--x)

            {

            g.drawImage(easteregg,x,1,this);

            // chicken.play();

           

           

    }

  

   

    g.setColor(Color.red);

    g.setFont(new Font("Arial", Font.PLAIN, 30));

    g.drawString("Happy Easter",200,100);

   

    g.setColor(Color.blue);

    g.setFont(new Font("TimesRoman", Font.ITALIC, 20));

    g.drawString("ECOO 2003",100,150);

 

chicken.play();

   

        

}          

 

 

 

 

} // end of Easter class

 

 

 

You will note the use of “try” and “catch” in the thread example.  This code enables Java to check for input errors and other exceptions.  It is used in file handling as well.  Also, we see that to create a thread the programmer must create a “new” instance of a thread, involving once again the Java OOP environment.