9/12/2010

Interview ready notes/points - Java Threads

  • A thread is a single sequential flow of control within a process. A single process can thus have multiple concurrently executing threads.
  • To run a thread call the start() method on the instance of the thread class.
  • The thread's start() method registers the thread with "Thread Scheduler".
  • The "Thread Scheduler" resides in Operating System and JVM.
  • The thread's start() method puts the thread in ready state and makes the thread eligible to run.
  • When the thread runs it executes run() method of a thread.
  • A thread can execute it's own run() method or another objects run() method.
  • If you call start() method of a thread , it automatically calls the run() method.
  • If you extend your class to Thread class, you cannot extends to another class.
  • There are two ways to create a thread class one way is by extending Thread class or by implementing Runnable interface.
  • After completion of executing run() method a thread is considered to be a dead thread.
  • You cannot start a dead thread, but variables and methods can be called.
  • The interrupt() method forcibly puts the thread into dead state.
  • If there are many threads are waiting then there is no guaranty that which thread is going to called by the "Thread Scheduler".
  • The yield() method puts currently running thread in to ready state.
  • The sleep() and yield() methods runs only on currently threads.
  • If a sleeping thread receives interrupt() method call the thread immediately moves to ready state otherwise it throws an "InterruptedException".
  • Sometimes it may true that a thread might be waiting for input or operation to be performed before it can continue, this kind of waiting of a thread said to thread become blocked or "Blocking State".
  • A monitor is an object which contains some synchronized code in it.
  • The keyword "synchronized" is used to mark entire method or block of code as synchronized.
  • The java.lang.Object class methods wait(), notify() and notifyAll() will be called from only in "synchronized" code. These methods are used in thread communications.
  • If you call wait() method on a thread it releases CPU, releases objects lock, the thread enters into pool of waiting threads.
  • Calling notify() method the waiting thread moves out of the waiting pool to ready state , but there is no guaranty which thread will be moved out of the pool.
  • Calling notifyAll() moves all waiting threads will be moved out of the waiting pool to ready state.

0 comments:

Post a Comment