Multithreading Flashcards

1
Q

What is a thread in java?

A

A thread is a lightweight process that runs within another process or thread. It is an independent path of execution in an application.

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

What is the priority of a thread and how is it used in scheduling?

A

Every thread has a priority between 1-10. Scheduler uses pre-emptive scheduling so a thread with higher priority gets preference over a lower priority thread.

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

What is the default priority of a thread?

A

A new thread gets the same priority as the priority of the parent thread. Default priority of a thread is 5.

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

What are the different priorities that can be set?

A

MIN_PRIORITY: 1
NORM_PRIORITY: 5
MAX_PRIORITY: 10

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

What is the purpose of the join() method in java?

A

We can use the join() method on a thread to make current thread wait for another thread to finish. When we use join(), the current thread stops executing. It waits for the thread on which join() was called to finish.

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

What is the difference between wait() and sleep()?

A

When we call wait() the current threaded releases the monitor and goes into a waiting state. Then another thread calls notify() to wake it up.
When we call sleep() the current thread just sleeps for some pre defined period of time.

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

What are the advantages of multithreading?

A

Improved performance, simultaneous access to multiple applications, reduced number of servers required, simplified coding.

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

What are the disadvantages of multithreading?

A

Difficult to debug, difficult to manage concurrency, difficult to port existing code into multithreaded code, deadlocks.

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

Can you lock an object for exclusive use by a thread?

A

Yes, using a synchronised block we can lock an object to a specific thread.

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

Difference between notify() and notifyAll()?

A

Notify is used to unblock a specific thread that is in waiting state. NotifyAll is used to unblock all waiting threads.

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

What is a daemon thread in java?

A

A low priority thread that does not prevent the JVM from exiting when the program finishes. Garbage collection is an example of a daemon thread.

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

What is synchronisation?

A

It is a feature in Java that helps in controlling the access of multiple threads to a shared resource.

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

What is the purpose of Synchronised block in java?

A

It can prevent thread interference, it is used to avoid memory inconsistency issues.

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

What is a deadlock?

A

A deadlock is a situation in which two or more threads are waiting on each other to release a resource. This can result in a universal waiting state.

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

What is the meaning of concurrency?

A

The ability of a program to execute several programs simultaneously. This is achieved by distributing computation over multiple CPU cores of a machine or over different machines within the same network.

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

Difference between a process and a thread?

A

A thread runs in a shared memory space, a process runs in its own space. A process runs in an environment provided by the operating system. A thread lives within a process and shares the resource like-memory.

17
Q

What is a scheduler?

A

A program that is the implementation of a scheduling algorithm to manage access of processes and threads to limited resources like CPU or I/O channel. The goal is to provide load balancing for the available processes/threads.

18
Q

What are the different states of a thread?

A

New: new thread created
Runnable: a thread executing in the JVM is in Runnable state
Blocked: a thread waiting for a monitor lock is in a blocked state
Waiting: a thread in a waiting state
Terminated: a thread that has exited

19
Q

How to create a thread?

A

Extend thread class: we can extend thread class and implement the run() method.
Implement runnable interface: we can implement runnable interface and pass the implemented object to the constructor of the thread class.

20
Q

What is an atomic operation?

A

An operation that completes in a single step relative to other threads. It is either executed completely or not at all.

21
Q

What is thread starvation?

A

Threads with lower priority get less time for execution than higher priority threads. Thread starvation occurs when a lower priority thread does not get enough time to finish computation.

22
Q

What is a race condition?

A

When a program attempts to perform two or more operations at the same time, but because of the logic of the program, the operations have to be performed in proper sequence to run the program correctly.