Concurrency Flashcards

1
Q

What is a thread?

A

A thread is a lightweight process. A thread can share memory with its peer threads. Threads require less time for context switching. All Java programs have at least one thread, known as the main thread, which is triggered when the program starts and the main method is executed.

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

How to create a new thread in Java?

A
  1. By extending Thread class
  2. By implementing Runnable interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do you prefer, implementing Runnable or extending Thread and why?

A

When we extend Thread class, we can’t extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now. When we extend Thread class, each of our thread creates unique object and associate with it. When we implement Runnable, it shares the same object with multiple threads. Therefore implementing Runnable should be the best choice in most cases.

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

What is Thread.sleep()?

A

Thread.sleep() is a method that can be used to pause the execution of current thread for specified time in milliseconds.

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

How to interrupt a thread?

A

thread.interrupt(); // calling interrupt() method

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

What is InterruptedException?

A

It is an exception thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity.

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

What is Thread.join()?

A

When we invoke the join() method on a thread, the calling thread goes into a waiting state. It remains in a waiting state until the referenced thread terminates.

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

Is there any problems with shared data in multi-threaded environment?

A

If two threads access a shared variable without any kind of guard, writes to that variable can overlap. Let’s say both threads add 1 to the same memory location; they do this by reading the value in the memory location into a register, incrementing it, then writing it back. Thread 1 (T1) reads the value (let’s say ‘0’ initially), increments it, and writes 1 back. After T1 has read it, but before it writes the value back, T2 reads the 0 value, and then writes 1 back after T1. So despite two increments, the value is 1, when it should be 2. The access has to be atomic, i.e., done in one operation, in order to prevent overlapping writes. This can be done in a number of ways. Java has java.util.concurrent.atomic.AtomicInteger with increment and decrement methods. An atomic operation is an operation which is performed as a single unit of work without the possibility of interference from other operations.

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

What is critical section?

A

Concurrent accesses to shared resources can lead to unexpected or erroneous behavior, so parts of the program where the shared resource is accessed need to be protected in ways that avoid the concurrent access. This protected section is the critical section or critical region. It cannot be executed by more than one process at a time. Typically, the critical section accesses a shared resource, such as a data structure, a peripheral device, or a network connection, that would not operate correctly in the context of multiple concurrent accesses.

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

Please explain synchronized keyword

A

All synchronized blocks are synchronized on the same object and they can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block. Synchronized can be applied to blocks or to entire methods.

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

What is a monitor when we call synchronised method?

A

This synchronization is implemented in Java with a concept called monitors. Only one thread can own a monitor at a given time. When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor.

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

What is a monitor when we call a synchronized static method?

A

In simple words a static synchronized method will lock the class instead of the object, and it will lock the class because the keyword static means: “class instead of instance”. The keyword synchronized means that only one thread can access the method at a time. Therefore, when a thread attempts to enter a synchronized static method, it will ask for the monitor of the Class Object that represents the class within which the static method exists.

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

What is a deadlock?

A

A deadlock is a condition that consists of two or more threads are waiting on each other for some action or resource.

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

Can I call wait or notify outside of a synchronisation block?

A
  1. The Producer thread tests the condition (buffer is full or not) and confirms that it must wait (after finding buffer is full).
  2. The Consumer thread sets the condition after consuming an element from a buffer.
  3. The Consumer thread calls the notify () method; this goes unheard since the Producer thread is not yet waiting.
  4. The Producer thread calls the wait () method and goes into waiting state.
    So due to race condition here we potential lost a notification and if we use buffer or just one element Produce thread will be waiting forever and your program will hang.

This race condition is resolved by using synchronized keyword and locking provided by Java. In order to call the wait (), notify () or notifyAll () methods in Java, we must have obtained the lock for the object on which we’re calling the method.

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

How does wait method work?

A

The wait() method causes the current thread to wait indefinitely until another thread either invokes notify() for this object or notifyAll().

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

How does notify method work?

A

For all threads waiting on this object’s monitor (by using any one of the wait() method), the method notify() notifies any one of them to wake up arbitrarily. The choice of exactly which thread to wake is non-deterministic and depends upon the implementation.

Since notify() wakes up a single random thread it can be used to implement mutually exclusive locking where threads are doing similar tasks, but in most cases, it would be more viable to implement notifyAll().

17
Q

What is the difference between notify and notifyAll?

A

This method simply wakes all threads that are waiting on this object’s monitor.

The awakened threads will complete in the usual manner – like any other thread.

But before we allow their execution to continue, always define a quick check for the condition required to proceed with the thread – because there may be some situations where the thread got woken up without receiving a notification (this scenario is discussed later in an example).

18
Q

Please explain volatile keyword

A

This keyword allows the JIT compiler to bypass the cache and access data directly from the memory avoiding situations where the data stored in the cache is an old version. With cache, changes are first applied inside the cache and then they are transferred to the memory. Not using cache however brings a performance penalty.

19
Q

What pros immutable classes have in context of multi-threaded applications?

A

The advantage of immutable objects is that you know their data cannot change, so you don’t have to worry about that. You can pass them around freely without having to remember whether a method you pass them to could change them in a way your code is not prepared to handle. That makes working with immutable data easier.

20
Q

Thread.stop()

A

This method was deprecated with Java 8. You should be using Thread.interrupt() which will throw an InterruptedException or you should use a flag with AtomicBoolean and with a while loop.

21
Q

What is the main difference between StringBuilder and StringBuffer?

A

StringBuffer is synchronized i.e. thread safe. It means two threads can’t call the methods of StringBuffer simultaneously.

StringBuffer is less efficient than StringBuilder.