Saturday, December 29, 2007

Threads Some Important Points

1. Methods in object class
· wait : throws interruptedException
· notify
· notifyAll

Remember : they are final and must be called from within a synchronized
context, otherwise illegalmonitorstate exception is thrown at runtime.


2. yield , sleep are static members of Thread class.


3. stop , resume , suspend are deprecated members of Thread class.


4. InterruptedException is checked exception.


5. Given
synchronized (expression){…..}
· expression may evaluate to reference to object.
· expression cannot evaluate to primitive type or null.
· If execution block completes ( abruptly or normally )the lock is released.
· synchronized statements can be nested.
· synchronized statements with identical expression can be nested.


6. Thread.yeild method
· may cause the current thread to move from runnable to ready state .
· It isn’t guaranteed i.e. same thread may start executing again.


7. Thread.sleep method
· makes thread to move from running to not – runnable state
· thread takes lock with it if it has got one.


8. Thread class inherits Runnable interface.


9. A dead thread can never be restarted.


10. When overriding the ‘run’ method of the Thread class remember the following
· the prototype must be public void run(){}
· Access modifier must be public.
· ReturnType must be void.
· No arguments must be specified and if done it doesn’t cause error but it can’t
be used to start a new thread.
. if the class implements Runnable interface then it has to possess run method
with no arguments specified otherwise compiler error is caused.
· it must not declare any new checked exception in its throws clause.
· it must not be declared static
· It can declare unchecked exception in its throws clause.
· If start method is called twice then illegalThreadStateException is thrown.


11. the run method in Thread class is empty method which does nothing.

12. join,sleep,wait methods declare InterruptedException in their throws clause.


13. If we extend thread class or implement runnable interface then the present run()
method must not be marked static ,if it is marked it causes compiler error.


14. The priority of a thread can be altered after the thread has been started using
setPriority method .The code compiles fine.


No comments:

Search Amazon for Best Books on Java J2EE


blogarama - the blog directory

Search your favourite topics