Explain the threading Jargon in Java

Carvia Tech | March 30, 2019 | 3 min read | 60 views


Race Condition

A race condition occurs when the correctness of a computation depends on the relative timing of multiple threads by the runtime. In this scenario Getting the right result relies on the lucky timings.

A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don’t know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data.

Problems often occur when one thread does a "check-then-act" (e.g. "check" if the value is X, then "act" to do something that depends on the value being X) and another thread does something to the value in between the "check" and the "act".

For example, in the below code, if another thread changed the x in between point 1 and point 2, then y will not be equal to 10.

if (x == 5) // The "Check"                              (1)
{
   y = x * 2; // The "Act"                                 (2)

   // If another thread changed x in between "if (x == 5)" and "y = x * 2" above,
   // y will not be equal to 10.
}

Race condition can be avoided by proper understanding of Java Memory Model. Properly synchronizing the shared resource will not allow more than one thread to alter the state of shared resource in indeterministic manner.

Dead Lock

Dead lock occurs when two or more threads are blocked forever, waiting for each other to release up the shared resource. For two threads, it happens when two threads have a circular dependency on a pair of synchronized shared resources.

Starvation

Describes a situation where a thread is unable to gain regular access to shared resource and is unable to make any progress This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.

Mutex

Mutex stands for mutually exclusive, only one kind of operation (READ or WRITE) is allowed at a given time frame.

Live Lock

A thread often acts in response to the action of other threads, if the other thread’s action is also in response to another thread, then live lock may result. No progress but threads are not blocked.

Synchronizer

A synchronizer is any object that coordinates the control of flow of threads based on its state. For example, semaphore, CountDownLatch, FutureTask, Exchanger, CyclicBarrier, etc.

Latch

A synchronizer that can delay the progress of threads until it reaches the terminal state.

Semaphore

Counting semaphore are used to control the number of threads that can access a certain shared resource or perform a given action at the same time. Semaphores are normally used to implement resource pool or to impose a bound on a collection.

Example Usecases -
  1. Considering iText PDF conversion to be a cpu intensive task, do not allow more than 10 itext pdf conversions to run at any given time.

  2. There is a arbitrary algorithm in your program that takes lot of memory, do not allow more than 2 instances of this algorithm to run in parallel otherwise JVM will start throwing Out Of Memory Errors.

Exchanger

A two party barrier in which the parties exchange data at the barrier point.


Top articles in this category:
  1. What is ThreadLocal in Java, where will you use this class
  2. What will happen if we don't synchronize getters/accessors of a shared mutable object in multi-threaded applications
  3. What is Deadlock in Java? How to troubleshoot and how to avoid deadlock
  4. What is difference between sleep() and wait() method in Java?
  5. What do you understand by Java Memory Model?
  6. Can two threads call two different synchronized instance methods of an Object?
  7. What is volatile keyword in Java


Find more on this topic:
Core Java image
Core Java

Core Java - OOP Concepts, Garbage Collection, Multi-threading, Collections Framework, Java 8 Features, Lambda Functions, Streams.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info