By Russell Walker,2014-09-17 15:25
10 views 0




日期,7/3/2001 10:03:12 AM

7) Threads

Objective 1)

    Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable

? Java is fundamentally multi-threaded.

    ? Every thread corresponds to an instance of java.lang.Thread class or a sub-class.

    ? A thread becomes eligible to run, when its start() method is called. Thread scheduler co-

    ordinates between the threads and allows them to run.

    ? When a thread begins execution, the scheduler calls its run method.

Signature of run method public void run()

    ? When a thread returns from its run method (or stop method is called deprecated in 1.2), its

    dead. It cannot be restarted, but its methods can be called. (its just an object no more in a running state)

    ? If start is called again on a dead thread, IllegalThreadStateException is thrown.

    ? When a thread is in running state, it may move out of that state for various reasons. When it becomes eligible for execution again, thread scheduler allows it to run.

? There are two ways to implement threads.

1. Extend Thread class

    public class Thread extends Object implements Runnable


     Thread(Runnable target)

     Thread(Runnable targe, String name)

     Thread(String name)

     Thread(ThreadGroup group, Runnable target)

     Thread(ThreadGroup group, Runnable target, String name)

     Thread(ThreadGroup group, String name)

    ? Create a new class, extending the Thread class.

    ? Provide a public void run method, otherwise empty run in Thread class will be executed.

? Create an instance of the new class.

? Call start method on the instance (dont call

run it will be executed on the same thread)

2. Implement Runnable interface

public interface Runnable

     only one method: public void run

    ? Create a new class implementing the Runnable interface.

? Provide a public void run method.

? Create an instance of this class.

    ? Create a Thread, passing the instance as a target new Thread(object)

    ? Target should implement Runnable, Thread class implements it, so it can be a target itself.

? Call the start method on the Thread.

    ? JVM creates one user thread for running a program. This thread is called main thread. The main method of the class is called from the main thread.

    It dies when the main method ends. If other user threads have been spawned from the main thread, program keeps running even if main thread dies. Basically a program runs until all the user threads (non-daemon threads) are dead.

    ? A thread can be designated as a daemon thread by calling setDaemon(boolean) method. This method should be called before the thread is started, otherwise IllegalThreadStateException will be thrown.

    ? A thread spawned by a daemon thread is a

daemon thread.

    ? Threads have priorities. Thread class have constants MAX_PRIORITY (10), MIN_PRIORITY (1), NORM_PRIORITY (5)

    ? A newly created thread gets its priority from the creating thread. Normally itll be


? getPriority and setPriority are the methods to

    deal with priority of threads.

    ? Java leaves the implementation of thread scheduling to JVM developers. Two types of scheduling can be done.

1. Pre-emptive Scheduling.

Ways for a thread to leave running state -

    ? It can cease to be ready to execute ( by calling a blocking i/o method)