Threads Flashcards

1
Q

Process vs Thread

A

A process is a program under execution, performing a set of actions specified in the program. In contrast, a thread is a subset of the process, also known as the lightweight process. Proper synchronization is required in the case of threads, but in the case of process, there is no need for synchronization as they work in an isolated manner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Thread Pool Concept

A

A thread pool is a collection of pre-initialized threads. Generally, the collection size is fixed, but it is not mandatory. It facilitates the execution of N number of tasks using the same threads. If there are more tasks than threads, then tasks need to wait in a queue like structure (FIFO – First in first out). When any thread completes its execution, it can pickup a new task from the queue and execute it. When all tasks are completed, the threads remain active and wait for more tasks in the thread pool. A watcher keeps watching the queue (usually BlockingQueue) for any new tasks. As soon as tasks come, threads start picking up tasks and executing them again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Creating Threads

A

In Java, we can create a Thread in following ways: By extending Thread class By implementing Runnable interface Using Lambda expressions Runnable subTaskWithLambda = () -> { System.out.println(“SubTaskWithLambda started…”); };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Context Switching

A

Context switching is a technique that includes saving the current state of a running thread, restoring it later when it has to be executed, and giving a chance to other waiting threads to execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Executor Framework

A

Since Java 5, the Java concurrency API provides a mechanism Executor framework. The main pieces are Executor interface, its sub-interface ExecutorService and the ThreadPoolExecutor class that implements both interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Thread Pool Executor

A

With ThreadPoolExecutor, we only have to implement the Runnable objects and send them to the executor. It is responsible for executing, instantiating, and running the tasks with necessary threads. We can create the following 5 types of thread pool executors with pre-built methods in java.util.concurrent.Executors interface. ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newScheduledThreadPool(10); Creates a thread pool that can schedule commands to run after a given delay or to execute periodically. ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newSingleThreadExecutor(); Creates a single thread to execute all tasks. Use it when you have only one task to execute. ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newWorkStealingPool(4); The Java Work Stealing Thread Pool Executor, introduced in Java 8, is a type of executor service designed to enhance performance in multi-threaded applications by dynamically balancing the load among threads. It is implemented by the ForkJoinPool class in the java.util.concurrent package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Concurrency vs Parallelism

A

Concurrency is when multiple tasks start, run, and complete with each other to progress in overlapping time periods, in no specific order. Concurrency is essentially applicable when we talk about a minimum of two tasks or more. When an application is capable of executing two tasks virtually at the same time, we call it a concurrent application. Parallelism does not require two tasks to exist. It, literally, physically runs parts of tasks OR multiple tasks, at the same time using the multi-core infrastructure of the CPU, by assigning one core to each task or sub-task. In a single-core CPU, we may get concurrency but NOT parallelism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Thread States

A

During its lifetime, a thread can be in the following states: NEW RUNNABLE (Ready or Running) WAITING TIMED WAITING BLOCKED DEAD or TERMINATED

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Thread States

A

When you create an instance of the Thread class, the thread is in the NEW state When you call the start() method on that thread class object, the thread is in the RUNNABLE (Ready to Run) state. In this state, the thread is waiting for the thread scheduler to assign the processor to it. When the thread acquired the processor from thread-scheduler it starts executing the run() method, and it’s in the RUNNABLE (running) state. On the running thread, if you call sleep(time), join(time) or wait(time), the thread enters into the TIMED WAITING/WAITING (no time) state While calling notify() or notifyAll() method-At this stage, the thread is in the BLOCKED state. Once the thread acquires the lock then it moves to the RUNNABLE (Ready to Run) state If the run() method completes or we call the stop() method on the running thread, the thread enters into the DEAD or TERMINATED state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Thread Priority

A

Thread priority is an integer number between 1 to 10 assigned to each thread in the JVM.While allocating the processor to the thread, the thread scheduler gives the first chance to the thread with the highest priority. The default priority for any thread is inherited from the parent thread. As the default priority for the main thread is 5, so all child threads will have the default priority of 5.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Thread vs Daemon Thread

A

The daemon threads, generally, are created by the JVM with low priority than user threads and are executed in the background to support the user threads for smooth execution of the program. For example, garbage collection is a daemon thread started by JVM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Thread Group

A

Each thread in Java always belongs to a thread group. We can define the thread group of a new thread explicitly using the Thread class constructor.If we don’t define thread group explicitly, then the default thread group is inherited from the parent thread’s thread group.The purpose of ThreadGroup is to perform common activities for all threads present inside that thread group, like interrupting all threads, suspending all threads, setting max priority, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

start() method

A

Yes, we can override the Thread.start() method, but the overridden method will be executed just like a normal method call executed by the main thread only. It will not spawn a new Thread similar to the default start() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

run() method

A

If we don’t override the run() method in the extended class then the default Thread.run() method will be executed which has an empty body. Hence a new Thread will be created and executed which does nothing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Fairness Policy - Reentrant Lock

A

In concurrency, a fairness policy means that different threads can advance whatever they are doing. In a 100% fairness policy, all threads should advance their work in almost equal portions.Due to an unfair waiting policy, a thread can be waiting infinitely to acquire a lock and face starvation. To avoid this starvation problem, the lock must follow a fair waiting policy which says that the longest waiting thread must get the higher priority and should acquire the lock first, which in-directly means that threads must acquire the lock in the order they requested it. To provide fairness, java.lang.ReentrantLock class defines a constructor with a boolean parameter named fair. By default, the fairness policy is set to false which means the lock does not guarantee any particular access order. When set true, under contention, locks favor granting access to the longest-waiting thread. ReentrantLock(boolean fair)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Synchronized Block, Method and Statement

A

We can declare a method as synchronized to achieve synchronization, i.e only one thread can call that method on the same object. Which requires a thread to get the object level lock to execute that method. When synchronized is used for a static method, i.e. only one thread can call that method on any instance of a class. It requires a thread to get the class-level lock to execute that method. We can declare a block inside a method using the synchronized keyword to achieve synchronization for only some statements of the method present inside the synchronized block. synchronized(this){…}

17
Q

synchronized method as abstract

A

In general, synchronization requires a lock to be acquired before a method can be executed, and a lock is an implementation feature. Since an abstract method does not have an implementation (and lock), it cannot be synchronized.

18
Q

yield()

A

yield() The yield() method called on any thread pauses the currently executing thread as it’s not doing any critical or important operations and gives the chance to other waiting threads with the same or higher priority. public static native void yield(); As it’s the static method we can call the yield() method directly using the class name. Thread.yield(); Calling the yield() method will move the thread from the RUNNABLE (Running) to the RUNNABLE (Ready to Run) state.

19
Q

join

A

The join() method is used to wait for some other thread (on which the join() method is called) to complete its execution. After completion of that thread, the waiting thread can continue or start its execution. The join() method is defined in Thread class as follows: Calling the join() method will move the thread from the RUNNABLE (Running) to the WAITING state and calling the join(int millis), join(int millis, int nanos) method will move the thread from the RUNNABLE (Running) to the TIMED WAITING state.

20
Q
A