eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Web Programming
Threads  
  • At this stage in the development of computers, even the most casual user is familiar with multi-tasking. Multi-tasking is the abilty to run several applications (or "processes") at the same time, often in separate windows. This is a very powerful feature because it allows you to do several things at one time. Or more corerctly, it allows the computer to do several things at the same time. After all the computer has quite a bit of power at its disposal and can perform many functions while you are not keeping it busy. Multi-tasking allows it to do just this. While you are staring at your monitor thinking how best to say what you want to say in a document you are writing, the CPU can be utilized by other applications for other processing, such as complex math, or printing, or faxing, or emailing.

  • Multi-tasking works because applications are allowed to "share" the CPU time.

  • Well the concept of multi-tasking can be taken one step further than simply supporting multiple processes at one time. When applied to a single program, the concept of multi-tasking is called multi-threading and is one of the most important features of Java.

  • Multi-threading focusses on multiple, simultaneously-executing "threads" within a single application rather than multiple, simultaneously-executing applications.

  • The benefit of a multi-threaded application is similar to that of a multi-tasked operating system. Your application can do a lot of things by using separate threads.

  • For example, you can easily load images (typically a time hog operation), in the background while the user goes about doing other things in the application. That way, the user does not need to sit and wait for images to load.

  • Unlike the multiple processes in a multi-tasked environment which each comprise their own environemnt with their own variables, their own methods, and perhaps their own objects however, the threads in a multi-threaded system are all contained within the environment of the application they are running in.

  • Thus, threads share all the same resources (variabls, methods, objects) as all the other ones do. In fact, they are all simply children of the main application which controls the dynamics of the separate threads.

Single Thread Lifecycle

  • So let's look at a thread to see how it operates.

  • All threads are instantiated from the java.lang.Thread class and have a host of public methods which are all available and described in the online documentation.

  • To create a thread you will need to implement the the Runnable interface which invloves defining a run() method such as in the following example:

    public class MyThreadClass implements Runnable
      {
      public void run()
        {
        Code to execute within the thread
        }
      }
    

  • Once you have created a thread class with a run() method, you can easily instantiate it using something like the following:

    MyThreadClass myThread = new MyThreadClass();
    Thread thread = new Thread(myThread);
    

  • Notice that we passed our thread as a parameter to a Thread object. This is important because the thread object will act as an overseer for our thread. Thus, when we want to start, stop or destroy our thread we will do so through the Thread object wraper. but what can you do with a thread? Well, there are three primary things you will do in a threads life:

Method Description
void start() Creates a thread object
void stop() Stops a thread
void destroy() Sends the thread to the garbage collector

  • Thus, to expand our example above, we might see:

    MyThreadClass myThread = new MyThreadClass();
    Thread thread = new Thread(myThread);
    thread.start(); //calls our run() method.
    thread.stop(); //stops the thread's execution
    thread.destroy(); // trashes thread.
    

Multiple Threads

  • Actually, it is not quite corerct to say that in the above example you are working with a single thread. In fact, threads are so essential to Java that every program uses them. That is, you always have at least one thread, your main thread which executes your program. When such a program instantiates another thread there are two of them running, the second one reporting to the first.

  • As you might imagine, you are certainly not limited to two threads either. In fact you can have as many threads as your CPU can manage. Each thread can perform its own tasks independently, sharing the resources of the main thread.

  • That last idea is crucial. All threads share the same global variables and methods. Thus, if one thread changes the color of a Label to green, then all threads are effected.

  • The fact that threads can affect the same properties of course, means that you must be very careful as to how those resources are shared. for example, ou must make sure that two or more threads do not access the same resource at the exact same time. If this were to happen, your data would be corrupted. Typically, this is dealt with by using locks on data objects through the "synchronized" keyword. However, when locking data, you must make sure that you do not create a deadlock situation in which two objects enter an endless loop because a thread calls another thread within a locked area and the second thread needs to get at the locked data. The intial thread will never relinquich the lock since it is waiting for the waiting thread!

  • Finally, Java allows you to set various levels of priority to multiple threads such that some gain more CPU time than others.

  • Since threads are extremely complex, so much so that I hesitate to try to address them here, I recommend that you pick up a book solely dedicated to threads programming if you want to get a better feel for the intricacies.

Previous Page | Next Page