Multithreading Flashcards
What is a thread in java?
A thread is a lightweight process that runs within another process or thread. It is an independent path of execution in an application.
What is the priority of a thread and how is it used in scheduling?
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.
What is the default priority of a thread?
A new thread gets the same priority as the priority of the parent thread. Default priority of a thread is 5.
What are the different priorities that can be set?
MIN_PRIORITY: 1
NORM_PRIORITY: 5
MAX_PRIORITY: 10
What is the purpose of the join() method in java?
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.
What is the difference between wait() and sleep()?
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.
What are the advantages of multithreading?
Improved performance, simultaneous access to multiple applications, reduced number of servers required, simplified coding.
What are the disadvantages of multithreading?
Difficult to debug, difficult to manage concurrency, difficult to port existing code into multithreaded code, deadlocks.
Can you lock an object for exclusive use by a thread?
Yes, using a synchronised block we can lock an object to a specific thread.
Difference between notify() and notifyAll()?
Notify is used to unblock a specific thread that is in waiting state. NotifyAll is used to unblock all waiting threads.
What is a daemon thread in java?
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.
What is synchronisation?
It is a feature in Java that helps in controlling the access of multiple threads to a shared resource.
What is the purpose of Synchronised block in java?
It can prevent thread interference, it is used to avoid memory inconsistency issues.
What is a deadlock?
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.
What is the meaning of concurrency?
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.