Saturday 10 January 2015

SCJP notes: multithreading


Miscelanious
  • If you want to write the code that will be started in its own thread, you can: or implement the Runnable interface; or extend the Thread object
  • To start the code to run its own thread, just call the start() method (not the run())
  • Thread itself implements Runnable
  • If you will to start already started Thread you will get IllegalThreadStateException
  • There is now way to pass the Thread priority, to the Threads constructor, you can pass only the thread name
  • To pass the priority to the thread just use the setPriority() method
  • Thread priority could be from 1 to 10
  • Priorities could be ignored by JVM
  • If priority of started Thread wasn't specified, the priority would be inherited from the priority of the Thread that stated Thread
java.lang.Thread important methods
  • To get the current thread instance you should call the Thread.getCurrentThread() method
  • The static Thread.sleep() method throws checked InterruptedException when thread encounters sleep it must go to sleep for at least to the number of specified milliseconds
  • The static Thread.yeld() method could be used as the hint to the scheduler that the current thread is willing to yield its current use of a processor and if it was successful in it other thread will run. But it is absolutely not guarunteed
  • The non-static join() method  throws checked InterruptedException makes the current thread wait until the Thread on which instance it was called will finish its work. If Thread on which instance it was called wasn't started or already dead nothing happens
  • The non-static interrupt() method if interrupted thread in the sleeping or waiting state cause the sleeping thread throw InterruptedException. Else just change the flag isInterrupted() to true. . Possible to interrupt not started thread and then start it. The isInterrupted() method will return false
  • The non-static setDaemon() method, sets the method as daemon. Program won't wait this thread to be finished
Synchronization
  • The method or the block of code could be synchronized and you need to synchronize when there is more than two thread in your application trying to access the same the data and at least one thread modifies this data
  • Synchronizing the instance method will cause the synchronization only for this one instance
  • The static method or the block of code could be synchronized on the static variable, so only one thread for the all application could execute such code at once
  • Synchronization of the non-static getter or setter for the static field will ensure you from race condition only when you use only one instance. When there is two or more instances synchronization could be broken
  • Synchronization of the static and non-methods doesn’t block each other
  • Sleep doesn't release the synchronization lock
  • The all run() method could be syncromized
 java.lang.Object important methods
  • wait(), notify(), notifyAll() must be called from within a synchronized context! A thread can't invoke this method on an object unless its own that object's lock. You will get the IllegalMonitorStateException if you will try it
  • The non-static wait() method  throws checked InterruptedException release the lock of synchronization and makes the thread to waits until it will notified or interrupted, or until timeout is passed if it is specified
  • There is  possibility that the thread will spontaneously wakes up it is so called "spurious wake ups"
  • The non-static method notify() wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is randomly chosen to be awakened. If nobody waits this monitor - nothing happens

No comments:

Post a Comment