Threading Model   «Prev 


Alternative Names for Java Thread States

The names of the thread states are formally defined in the Java Language Specification here Java SE 8 (Thread States).
Java programmers and authors refer to these states by different names.
As previously mentioned in the introduction page Java Threading Model, the term Runnable is the modern correct term for the previous term "ready".
The created state is often referred to as the New state, and the alive state is used to refer collectively to the Runnable (ready), running, and waiting states.
The alive state is broken down into the runnable and not runnable states.
The Runnable state corresponds to ready and Running states.
The non- runnable state corresponds to the waiting state. The following mapping should help you translate between these two sets of thread state names:

New Created
Alive Ready, Running, or Waiting
Runnable Ready or Running
Not Runnable Waiting

Concurrently execute method on same ThreadSafety instance

public class ThreadSafety{
  List< Student> sList = new ArrayList< Student> ();
  public Student getStudent(){
    Student s = sList.remove(0);
    return s;
  }
  //...other irrelevant code
} 
If two threads, T1 and T2, concurrently execute getStudent() method on the same ThreadSafety instance, which of the following are possible outcomes? (Assume that there are no nulls in the list.)
[ Select 3 options:]
  1. T1 gets a Student object and T2 gets IndexOutOfBoundsException.
  2. T1 and T2 get two different instances of Student objects.
  3. T1 gets a Student object and T2 gets null.
  4. T1 gets a Student object and T2 gets a ConcurrentModificationException
  5. Both the threads get a reference to the same Student object.


Answer: a, b, e
Explanation:
  1. If there is only one object in the list, it is possible that T1 gets the object, and when T2 tries to call remove(0), it gets IndexOutOfBoundsException because the list is now empty. Observe that in this situation, this is expected behavior. The exception is not because of T1 and T2 stepping on each other (though that might occur as well).
  2. This is an error free situation when both the threads are interleaved such that they do not impact each other.
    T1 removes an object and then T2 removes another object from the list. So both get a different object and there is no exception. Observe that thread unsafe code does not necessarily break all the time. It breaks at random.
  3. This will not happen because if there is no object in the ArrayList, the remove method does not return null but throws an IndexOutOfBoundsException.
  4. ConcurrentModificationException is thrown by the methods of the Iterator interfaces. It is thrown when, while one thread is iterating through a collection, another thread modifies the collection. In this case, no thread is iterating through the collection.
  5. Since the code accesses the list in an unsafe manner, it is possible that this situation might occur. It really depends on the implementation of ArrayList.
This is an example of a thread unsafe code. ArrayList methods are not synchronized and therefore multiple threads can execute its methods at the same time thereby corrupting its internal variables. This can yield unpredictable results for various method calls on the list object.