- 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
Method |
Description |
void start() |
Creates a thread
object |
void stop() |
Stops a thread |
void destroy() |
Sends the thread to the
garbage collector |
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
|