BILKENT UNIVERSITY
CS533 Information Retrieval Systems
Technical Presentation Web Site
Barýþ UZ
 
Index of
this page
Introduction
Push Technologies
Channels
CGI
ASP
Java
Streaming Media
 
Overview
 Classes
Main Data Structures in Java
Object Oriented Programming
More about Object Oriented Programming
Applets
GUI
Images and audio
Multithreaded Programming
Links
 
Definition of a thread: What is it ?
A thread can be defined as a separate stream of execution. It can be thought as a classical program which runs in a separate place and independent of everything else that might be happening. Threads are usually used for CPU intensive tasks. 

Threaded environments like Java allows threads to put locks on shared resources so that while one thread is using a shared resource, the others cannot use it. The others should wait for the lock to be released. 

Threads are generally used for calculation tasks which may take long time to complete. In addition, we use threads to divide the computer's power among the different tasks. While one thread is updating the display, the other may be computing the next display. 
 
Thread Class
We can create threads by creating a subclass of java.lang.Thread or when we want to implement our code segments as threads, we use java.lang.Runnable interface. 

There are three main controls exist to control threads: 

    public native synchronized void start() 
    public void run() 
    public final void stop()
The start() method prepares a thread to be run; the run() method actually performs the work of the thread; and the stop() method halts the thread. The thread dies when the the run() method terminates or when the thread's stop() method is invoked. 

You never call run() explicitly. It is called automatically by the runtime as necessary once you've called start(). There are also methods to supend and resume threads, to put threads to sleep and wake them up, to yield control to other threads, and many more. I'll discuss these later. 

The Runnable interface allows you to add threading to a class which, for one reason or another, cannot conveniently extend Thread. It declares a single method, run(): 

    public abstract void run()
By passing an object which implements Runnable to a Thread() constructor, you can substitute the Runnable's run() method for the Thread's own run() method. More properly the Thread object's run() method simply calls the Runnable's run() method. 
 
Thread Priorities
Not all threads are created equal. Sometimes you want to give one thread more time than another. Threads that interact with the user should get very high priorities. On the other hand threads that calculate in the background should get low priorities. 

Thread priorities are defined as integers between 1 and 10. Ten is the highest priority. One is the lowest. The normal priority is five. Higher priority threads get more CPU time. 

Warning: This is exactly opposite to the normal UNIX way of prioritizing processes where the higher the priority number of a process, the less CPU time the process gets. 

We can use Thread.MAX_PRIORITY, Thread.MIN_PRIORITY and Thread.NORM_PRIORITY constants if we don't want to bother with numbers. 

 

 
PREVIOUS TOPIC
UP
(First page of this topic)
NEXT TOPIC
PREVIOUS PAGE 
(of this topic)
NEXT PAGE 
(of this topic)