MY mENU


Tuesday 13 March 2012

Threads points



Some Important points about Threads:
  • A Java program runs until the only threads left running are daemon threads.
  • A Thread can be set as a user or daemon thread when it is created.
  • In a standalone program, your class runs until your main() method exists - unless your main() method creates more threads.
  • You can initiate your own thread of execution by creating a Thread object, invoking its start() method, and providing the behaviour that tells the thread what to do. The thread will run until its run() method exists, after which it will come to a halt - thus ending its life cycle.
  • The Thread class, by default, doesn’t provide any behaviour for run().
  • There are two ways to provide the behaviour for a thread
  • Subclass the thread and override the run() method - see p.253 of Exam Guide.
  • Implement the Runnable interface and indicate an instance of this class will be the thread’s target.
  • A thread has a life cycle. Creating a new Thread instance puts the thread into the “new thread” state. When the start() method is invoked, the thread is then “alive” and “runnable”. A thread at this point will repond to the method isAlive() by returning true.
  • The thread will continue to return true to isAlive() until it is “dead”, no matter whether it is “runnable” or “not runnable”.
  • If 2 threads are alive, with the same highest priority, the JVM switches between them. The JVM will switch between any number of threads with the same highest priority.
  • The priority numbers for threads falls between the range of Thread.MIN_PRIORITY and Thread.MAX_PRIORITY.
  • The default thread priority is Thread.NORM_PRIORITY.
  • New threads take on the priority of the thread that spawned them.
  • You can explicitly set the priority of a thread using setPriority(), and you can get the priority of a thread using getPriority(). There is no constructor for thread which takes a priority.
  • The JVM determines the priority of when a thread can run based on its priority ranking, but this doesn’t mean that a low priority thread will not run.
  • The currently executing thread can yield control by invoking yield(). If you invoke yield(), Java will pick a new thread to run. However, it is possible the thread that just yielded might run again immediately if it is the highest priority thread.
  • There are 3 types of code that can be synchronized: class methods, instance methods, any block of code within a method.
  • Variables cannot take the synchronized keyword.
  • Synchronization stays in effect if you enter a synchronized method and call out to a non-synchronized method. The thread only gives up the monitor after the synchronized method returns.
  • Using a thread’s stop() method will make it die.
  • There are 3 ways to transition a thread between “runnable” and “not runnable”
  1. put it to sleep and wake it up (not on the test)
  2. pause it and resume it (not on the test)
  3. use the methods wait(), notify() and notifyAll() - these are methods of class Object. wait() throws InterruptedException.
  • The third method listed above allows communication between the threads, whereas the other 2 methods don’t.
  • By using the methods wait(), notify() and notifyAll(), any thread can wait for some condition in an object to change, and any thread can notify all threads waiting on that object’s condition that the condition has changed and that they should continue.
  • When a waiting thread pauses, it relinquishes the object’s monitor and waits to be notified that it should try to reacquire it.
  • If you know you only have one thread waiting on a condition, you can feel free to use notify(), otherwise you should use notifyAll(). The notifyAll() method wakes up all threads waiting to reacquire the monitor for the object.
  • The reasons why a thread might be “alive” and “not runnable” are as follows:
  • The thread is not the highest priority thread and so is not able to get CPU time
  • The thread has been put to sleep using the sleep() method (of class Thread. Throws InterruptedException)
  • The thread has been suspended using the suspend() method
  • The thread is waiting on a condition because someone invoked wait() for the thread
  • The thread has explicitly yielded control by invoking yield()

No comments:

Post a Comment