Thursday, September 15, 2016

Thread

  1. Thread can be implemented either extending Thread Class or implementing Runnable Interface. Since we can’t extend 2 classes in Java implementing Runnable is better option
  2. States of Thread
·         New: The thread is created
·         Runnable: thread is runnable only after start() method
·         Running: thread is executing after run() method
·         Waiting/Blocked: thread is waiting for other thread to finish job. Once wait is over, it’s state is changed to runnable and it’s moved back to runnable thread pool
·         Dead: once thread finished executing, it’s state is changed to dead and it is not considered alive
·         start() and run(): start() internally calls the run() method. While we use start() method a new thread is created when run() is called internally to start(). But when we call run() method separately, it is executed in same thread.
  1. Object class has 3 final methods that allow thread to communicate about lock status of thread
·         wait(): has 3 variances. In one it waits indefinitely and call notify() notifyAll() method to wake up current thread. In other two the thread wait for a definite amout
·         notify(): wakes up only 1 thread waiting on an object and then that thread execution starts. If multiple threads are waiting on an object the method wakes up only 1 thread. Which thread will wake up is decided by OS implementation of thread management.
·         notifyAll(): wakes up all the threads waiting on the object. But which thread will execute first will depend on OS implementation. It is usually implemented in producer consumer problems.
·         join(): used to pause the current thread execution until unless specified thread is dead
thread1.join(); thread2.start() à thread two will start only after thread 1 is dead
thread1.join(2000); thread2.start() àthread two will start only after 2 sec or thread 1 is dead
  1. Message: A java class on which thread will work and call the wait() notify() methods
  2. Waiter: A class which will wait for other threads to invoke notify methods to complete its processing. Waiter thread is owning monitor on Message object using synchronized block
  3. Runnable and Callable: runnable from JDK1.0 while callable is from JDK 1.5. Callable’s call() can return value and throw exception which is not possible in Runnable’s run(). Callable return future object, which can hold result of a computation.
  4. For multithreaded environment thread safety a concern since updating an element is 3 steps process, taking the variable editing it and writing back. Threadsafety can be assured either by
·         Synchronization
·         Using atomic interger instead of ++
AtomicInteger atomicCount = new AtomicInteger( 0 );


  1. Thread safe by default in Java
·         Immutable objects like String
·         Read-only or final variables
·         Static variables if not initialized properly become major cause of thread-safety issues
·         Thread-safe classes like: Vector; Hashtable; ConcurrentHashMap; String
·         Atomic operation are thread-safe
·         Local variables are thread-safe
·         Volatile keyword for variables
  1. Also JVM plays a spoiler since it can reorder code for optimization, so the code which looks sequential and runs fine in development environment not guaranteed to run similarly in production environment because JVM may ergonomically adjust itself as server JVM and perform more optimization and reorder which cause thread-safety issues.

  1. When we create a Thread in java, by default it’s a user thread and if it’s running JVM will not terminate the program. When a thread is marked as daemon thread, JVM doesn’t wait it to finish and as soon as all the user threads are finished, it terminates the program as well as all the associated daemon threads.
        Thread dt = new Thread(new DaemonThread(), "dt");dt.setDaemon(true);

  1. Deadlock: when multiple threads are waiting for each other to release the resource thus causing cyclic dependency

  1. We can share data between two threads by using shared objects or concurrent data structures like Blocking queue. For ex. Consumer producer pattern. JavaBlocking Queue is a java Queue that support operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.

  1. Java thread pool manages the pool of worker threads, it contains a queue that keeps tasks waiting to get executed. We can use ThreadPoolExecutor to create thread pool in java.

No comments:

Post a Comment