-threads
-provide the only way to effectively
handle a number of tasks at once
-a thread is similar to a process in
an operating system
-if you have multiple threads, you need to have a
way to manage them and that is completed through synchronization
-allows you to stop 2 threads from modifying data at
the same time
-also allows you to make a certain part of a program
be atomic
-the Thread “object” in java represents a real
thread in the Java interpreter and serves as a handle for controlling and
synchronizing its execution
-how threads are executed:
-every
thread starts off when run() is executed
-the run() method takes no arguments and returns
nothing and it not allowed to throw an exception
-executed
by calling object.run().
-in order to have a runnable thread, the class needs
only implement the Runnable interface
-threads can also be executed by calling the start()
method, which can only be called once in the life of a thread
-the thread then continues to run until the run()
method is finished or until you call stop()
-when programming a thread, you can also just extend
Thread, which itself implements Runnable and then have the class start itself
by calling start() and making a run() method.
-the advantage to this is the run() method can then
access private variables and methods it might need in the class
-controlling threads:
-start()
brings the new Thread to life as described above
-there
are 3 other methods used to control threads:
-stop()
-suspend()
-resume()
-start()
and stop() can only be called once in the life of a thread
-the reasons for using suspend() and resume() would
be if it were very “expensive” to set up a new thread and destroy it.
-you can also call sleep() which will put the thread
to sleep for a given amount of time
-you can also call a method called interrupt() which allows a thread to
interrupt another thread
-NB:
This is not implemented in Java 1.0
-the very useful setDaemon(true)
method:
-if there are any threads that remain when the
application is otherwise complete, Java will kill the threads where
setDaemon(true) have been implemented
-only useful in actual java applications because in
applets the program runs inside another Java application and the daemon threads
it creates will continue to live until the controlling application exits – bad
-synchronization:
-the structures for implementing synchronization
activities are based on
”monitors” which are a widely used synchronization scheme developed by C.A.R
Hoare
-a monitor is a lock that is attached to a resource
that many threads may need access to but that only 1 thread should have access
to at a time
-all other threads have to wait until the current
thread gives up the resource
-in java, all you have to do is specify which
resources require locks
-the synchronized keyword marks places where a
thread must acquire the lock before proceeding
-when
threads are synchronized, only 1 thread will run at a time
-you can use the synchronized keyword to guard
arbitrary blocks of code as well – when this happens, the thread has to acquire
a lock on the object before proceeding
-wait()
and notify()
-by using these, a thread can give up it’s hold on a
lock at an arbitrary point and then wait for another thread to give the lock
back before continuing
-the thread will not wake up from a wait() if a
thread either doesn’t give up the resource or if the thread doesn’t call
notify()
-scheduling and priority
-every
thread has a priority value
-threads at the same priority level are scheduled
round robin but if a higher priority thread is started, it preempts the lower
priority thread and begins executing
-threads
continue running until one of the following happen:
-Thread.sleep() or Thread.wait() or called and the
thread sleeps
-the thread has to wait for a lock
-the thread blocks on I/O
-the thread explicitly yields control – yield()
-the thread terminates by reaching the end of run()
or by calling stop()
-for scheduling, the threads run on time slices,
though not all implementations use time slicing, most do
-thread processing is chopped up so that each thread
runs for a short period of time before the context is switched to the next
thread
-since time slicing isn’t mandatory, don’t write
thread code that depends on it
-priorities: MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY