SCJP – Threads

Dont forget

  • wait, notify or notifyAll must have the this object lock. So look if they are in a syncrhonized method

 

 

 

  • To analyze synchronized method issues, don't forget to verify if the threads are being created in same runnable object. If don't the synchronized methods locks will be distinct.
  • public void run() - is the signature of the method that must be extended to implement Runnable or to extend Thread
  • JVM exit after all the non demons threads created by the application complete, or when a thread executes System.exit().
    • if a main thread exits or if it throws a runtime exception, the child threads proceeds without any problem.
defined inThread defined inObject static final declare achecked exception Can throw aruntime exception
run X
start X
join X X
sleep X X
yield X X
currentThread X X
wait X X
notify X
notifyAll X

 

  • isAlive() - tests if this thread is alive. A thread is alive if it has been started and has not yet died.
  • If the thread doesn't own a lock on the object, and if we try to wait on this object, an IllegalMonitorStateException is thrown at runtime.
  • If a thread is waiting on another thread, and if those thread finishes the main thread is released without any problem.
  • synchronized block should be defined on an object: synchronized(this) {}
  • Thread.sleep causes the currently executing thread to sleep, even if called on a thread instance, not on the thread refereed in the code
  • Priority 1 is low priority, 5 is normal, 10 is maximum prioprity
  • interrupt and isInterrupted are Thread methods

 

  • The small execution block (means: don't allow to change running thread) - is per bytecode line, not by intermediate expression. To be confirmed
  • to a thread call wait, notify or notifyAll on a obj, the thread must own the lock for those obj.
  • Thread.sleep - don't release the lock
  • wait releases the lock

 

Synchronized  methods

  • public static SafeDeposit getInstance(int code) {} , with a private static instance and a private constructor
    • does not guarantees only one instance, because the getinstance is not synchronized

 

  • A class to be thread safe, the variables need to be private, and the get and set methods need to be synchronized

Tags:

Leave a Reply