Class 2 |
Intermediate Java 30-IT-397 |
|
Threads
-
Threads allow the computer to carry on multiple processes at one time.
-
Downloading e-mail while typing in Word, for instance.
-
With Java, we can have multiple processes in the same program
running at the same time.
-
Well, they actually don't run at the exact same time, unless you have multiple
processors. But they do appear to, because they use minute instances
of time of the PC.
-
These independent processes are called threads, or thread of control.
-
Threading can significantly improve performance of Java applications.
Java can use the time between keystrokes, while waiting for a connection,
etc. to perform other processes, such as garbage collection or event queueing.
-
Though nice and useful, they are tricky.
-
You could conceivably run two completely different Java programs on one
computer. If the two programs happen to have variables of the same
name, it's no problem, because they are actually different processes.
-
But threads are different. They are within the same program and share
the same variables. Thus, you must take precautions. We'll
talk about this later.
-
You could read entire books on threads. There is much more about
them than we can cover in this class. As you grow as a Java developer,
I would highly recommend devoting some time to reading about threads.
-
All that being said, threading in Java is much easier than other programming
languages.
-
Like it or not, your Java program already uses threads.
-
Garbage collection is handled via threads. Thus, memory management
is automatic, one of the best benefits of Java.
-
Collecting events in a GUI is performed in a separate thread.
Two Ways To Thread

Created by: Brandan Jones
December 17, 2001