Chapter 13: Threads
The book chapter is called "threads and animations" and has a lot to do with creating gif animations using Java.
http://java.sun.com/docs/books/tutorial/essential/threads/index.htmlBut actually, I am not too interested in using Java for animations, and there are better technologies for doing that such as Flash.
But I am interested in Threads, which are very fundamental to the language.
So this lesson is not based on the book, but on the Sun tutorial which is at
Threads can be difficult, and we are not getting into depth, but I want to introduce you to the concept early on so it doesn't come as a shock later.
(Show tutorial page with sorting applets)
Definitions:
a process is created by the OS as a "space" for running an application.
Every application you run in Windows consists of a process. Plus there are many other processes which are not applications.
(show the task manager)
In Windows and most OS, a process has its own protected memory space which is isolated from other processes.
A process may be single-threaded or multi-threaded.
If it is single-threaded, it has a series of execution instructions, which are executed one after the other.
If it is multi-threaded, it contains multiple threads:
Each thread is like a miniature process with its own series of execution instructions.
Each thread is mostly independent of the other threads running.
Threads share the same memory space, that is, they have access to all the objects in memory for the containing process.
Java and Threads
http://java.sun.com/docs/books/tutorial/essential/threads/DiningPhilosophers.htmlJava is a multi-threaded language.
The language supports the use of multiple threads in any application.
Some operating systems have their own native support for threads. The JVM can be configured to use this.
Some performance advantages especially in UNIX.
By default, the JVM uses its own thread model known as green threads.
This gets translated into a single sequence of instructions at the OS level, but to you, the programmer, it is multithreaded.
Every application has many threads running, even when you are not aware of it for example:
A main thread (where the main() method executes)
An event thread (where events are generated and handled)
A garbage collector thread (cleans up your mess)
Threads explicitly created by your application using the java.lang.Thread class
Why create a new thread?
There are several reasons, but generally it's to allow some background work to take place without affecting the responsiveness of the UI.
Here are some examples why I have created threads in applications:
To cancel a database search. Need a separate thread that can respond to a click event.
To preload Java objects while a database login process is taking place.
To listen for socket connections from other applications and respond to them.
Creating a new thread: two ways
First way: subclass the Thread class and override the run() method.
Example SimpleThread
Second way: implement the Runnable interface and write a run() method.
Example SimpleRunnable
In both cases, all the work is done in the run().
The thread starts when Thread.start() is executed. This causes run() to be executed.
The thread keeps going until it reaches a return or the run() method ends.
the thread is then dead and the garbage collector can collect it, along with any other objects referenced by the thread.
There is a way to kill a thread from another thread, but sun has deprecated this and you should never do it.
Instead, write the thread in such a way that it knows when to terminate itself.
Thread priority
Threads can have different levels of priority. At any given time, the JVM executes the thread with the highest priority.
Threads can be written in a selfish way.
Example: selfish.java
The thread just keeps gobbling up CPU and does not yield to other threads.
To be a better citizen, thread should occasionally call yield() to allow other threads to run.
Synchronizing threads
Often 2 threads need to use the same resource, whether it is an object in memory, a file on disk, etc. To avoid conflict the threads need to be synchronized.
Synchronizing a thread involves locking some object for the duration of a task and then unlocking it when done. Java does this for you when you use the synchronized keyword.
Example: producer-consumer
uses wait() and notifyAll()
Without the synchronization, the consumer might pull the same number twice in a row and might skip a number put in by the producer.
There's a thing called deadlock:
example
Thread groups
Threads can be grouped into ThreadGroups (java.lang.ThreadGroup)
We don't need to go into detail on that
That's about all I want to say about Threads right now. Any questions?