Java Threads Important Points.

Defining and instantiating and Starting Threads:

  • Threads can be created by extending Thread and overriding the public void run () method.
  • Thread objects can also be created by call the thread class constructor that takes a Runnable argument, the Runnable object is said to be the target of the thread.
  • You can call start () on a thread only once. If start() is called more than once on a thread object it will throw Runtime exception
  • It is legal to create many thread objects using the same Runnable object as the target.
  • When a thread object is created, it does not become a thread of until its start () method is invoked. When a thread object exists but hasn’t been started. It is in the new state and is not considered alive.

Transitioning between the Thread States:

  • Once a new thread is started, it will always enter the Runnable state.
  • The thread scheduler ca move thread back and forth between the Runnable state and the running state.
  • For a typical single-processer machine, only one thread can be running at a time, although many threads may be in Runnable state.
  • There is no guarantee that the order in which threads were stated determines the order in which they’ll run.
  • There’s no guarantee threads will take turns in any fair way. It’s up to the thread scheduler, as determined by the particular virtual machine implementation. If you want a guarantee that your threads will take turns regardless of the underlying JVM. You can use the sleep () method.
  • This prevents one thread from hanging the running process while another thread serves. (In most cases though yield () works well enough to encourage your threads to play together nicely).
  • A running thread may enter a blocked / waiting state because it can’t acquire the lock for a synchronized block of code.
  • When the sleep or wait is over, or an objects lock become available, the thread can only reenter the Runnable state. It will go directly from waiting to running (well, for all practical purpose only).
  • A dead thread cannot be started again.

Sleep, Yield and join:

  • Sleeping is used to delay execution fo a period of time, and no locks are released when a thread goes to sleep.
  • A sleeping thread guaranteed to sleep for at least the time specified in argument to the sleep () method (unless it is interrupted), but there is no guarantee as to when the newly awakened thread will actually return to running.
  • The sleep () method is static method that sleeps the currently executing thread’s state. One thread cannot tell another thread to sleep.
  • The set priority() method is used on thread objects to give threads a priority of between 1(low) and 10(high), although priorities are not guaranteed, and not all JVMs recognize 10 distinct priority levels, some levels may be treated as effectively
  • If not explicitly set, a thread’s priority will have the same priority as priority of the thread created it.
  • The yield () method may cause a running thread to blackout if there are Runnable threads of same priority.
  • There is no guarantee that those will happen, and there is no guarantee that the thread selected to run. A thread might yield and then immediately renter the running state.
  • The closest thing to guarantee is that at any given time when the thread is running it will usually not have a lower priority than a thread in the Runnable state.
  • If a low-priority thread running and a high-priority thread enter Runnable, the JVM will usually preempt the running low-priority thread and put the high-priority thread in.
  • When one thread calls join () method of another thread, the currently running thread will wait the thread it joins which has completed.
  • Think the join () method as saying “Hay thread, I want to join on to the end of you, let me know when you are done so I can enter the Runnable state.”

Concurrent Access Problems and Synchronized Threads:

  • Synchronized methods prevent more than one thread from accessing an object’s critical method code simultaneously.
  • You can use the synchronized keyword as a method modifier, or to start a synchronized block of code.
  • To synchronize a block of code ( in other words, a scope smaller than the whole method) you must specify an argument that is the object whose lock you what to synchronize code.
  • While only one thread can be accessing synchronized code of a particular instance multiple threads can still access same objects unsynchronized code.
  • When a thread gets to sleep, its locks will be unavailable to other threads.
  • Static methods can be synchronized, using the local from java.lang.Class instance representing the class.

Communicating with Objects by Waiting and Notifying:

  • The wait() method lets a thread say, “there is nothing for me to do now, so out me in your out pool and notify me when something happens that I care about”. Basically a wait () call means “wait me in your pool” or “add me to your waiting pool”.
  • The notify () method is used to send a signal to one and only one of the threads that are waiting in that same object’s waiting pool.
  • The notify method can NOT specify which waiting thread to notify.
  • The method notifyAll () works in the same way as notify (), only it sends the signal to all of the threads waiting on the object.
  • All three methods wait (), notify (), notifyAll () must be called for within a synchronized context. A thread invokes wait () or notify () on a particular object, and the thread must currently hold the lock on the object.

Deadlocked Threads:

  • Deadlocking is when thread execution grinds to a halt because the code is waitng for locks to be removed from objects.
  • Deadlocking can occur when a locked object attempts to access another locked object that is trying to access the first locked object.
  • In other words, both threads are waiting for each other’s locks to be released; therefore, locks will never released
  • Deadlocking is bad, don’t do it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s