Java Multithreading
What is Runnable interface ?
- An interface should be implemented by any class whose instances are intended to be executed by a thread - It has only one method- public void run() perform action for a Thread
What are the advantages of Multithreading ?
- doesn't block user -saves time by performing many operations at the same time -it is independent, which means doesn't affect other threads
What are the methods of object class used in inter-thread communication?
- wait() - notify() - notifyAll()
What is Synchronized Block ?
-A code block to perform synchronization on any specific resource of the method -used to Lock an object for any shared resource -scope of synchronization block is smaller than the method
How join() method works ?
-It waits a thread to die -throws InteruptedException
What will happen if you call run() method directly instead of through the start() method ?
-each thread starts a separate call stack -the run() method goes onto the current call stack rather than at the beginning of a new call stack
What is a Daemon Thread ?
-service provider thread -it's life depends on the user thread -it is low priority thread
What is sleep() method does?
-sleeps a thread for a specific amount of time -if you sleep a thread for a specific amount of time the thread scheduler picks up another thread
In how many ways can we create a thread ?
1. By extending a Thread class 2. By implementing Runnable interface
How can an object can be unreferenced ?
1. By nulling the reference 2. By assigning the reference to another object 3. By anonymous object
How process-based multitasking works?
1. Has its own address in memory 2. Cost of communication between process is high 3. Heavyweight 4. Switching between processes takes time
What are constants defined in the Thread class ?
1. MIN_PRIORITY 2. NORM_PRIORITY 3. MAX_PRIORITY
What are the life cycles of a thread ?
1. New state 2. Runnable state 4. Non-runnable 4. Terminated
What are the types of Synchronization?
1. Process Synchronization 2. Thread Synchronization
What are the ways to achieve multitasking ?
1. Process-based multitasking(multi-processing) 2. Thread-based multitasking (multi-threading)
How thread-based multitasking works ?
1. Threads share the same address space 2. Thread is lightweight 3. Cost of communication between threads is low
Why do we need to use Synchronization?
1. To prevent thread interference 2. To prevent inconsistency problems
What is a Thread class ?
A class which extends object class and implements Runnable interface
What is Thread Pool ?
A group of worker threads that are waiting for job and reuse many times
What is Synchronized method ?
A method used to Lock an object for any shared resource. When a thread invokes a Synchronized method, it automatically acquires the Lock for that object and release it when the thread completes the task
What is Multitreading?
A process of executing multiple threads simultaneously
What is a Thread?
A separate path of execution
What is the advantage of thread pool ?
Better performance
What does the setName() method do?
By default each thread has a name eg. thread-0, thread-1 and so on, setName() method used to change these names
What is Deadlock
Deadlock can occur in situation where a thread is waiting for an objects's Lock, that is acquired by another thread and the second thread is waiting for an object Lock that is acquired by the first thread.
What is thread priority ?
Each thread has a priority represented by number 1 and 10
What is Interrupting a Thread ?
If a thread is in sleeping or waiting state, calling the interrupt() method on the thread breaks out the sleeping or the waiting state throwing InterruptingException
What is Static Synchronization?
If you make any Static method as Synchronized, the Lock will be on the class not on the object
What will happen if you start a thread two times ?
IllegalThreadStateException will be thrown
How does wait() method used ?
It causes the current thread to release the Lock and wait until another thread invokes the notify() method or the notifyAll() method for this object
What is a Thread Scheduler ?
It is a part of JVM decides which thread to run
How does notify() method used?
It wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened
How does notifyAll() method is used ?
It wakes up all threads that are waiting on this object's monitor
What is Thread Group ?
Java provides a convenient way to group multiple threads in a single object. In such a way, we can suspend, resume, or interrupt group of threads by a single call.
What is Multitasking ?
Multiprocessing + Multithreading
What is the difference between preemptive and time-slicing scheduling ?
Preemptive- highest priority task executes until it enters the waiting or dead states or higher priority task comes into existence Time-slicing- a task executes for a pre-defined slice of time and then renter the pool of ready tasks.
What is the concept of Lock ?
Synchronization is built around an internal entity known as Lock or Monitor. Every object has a Lock associated with it. A thread that needs consistent access to an object's fields, has to acquire the objects Lock before accessing them and then release the Lock when it is done with them
What is Synchronization ?
The capability of controlling access of multiple threads to a shared resource
When do we say a thread is in runnable state ?
When a thread scheduler has selected it.
What does currentThread() do?
returns a reference of the currently executing object
What are the commonly used methods of a Thread class ?
run() start() sleep() join() getProperty() setProperty() getName() setName() stop() ...
What method used to start a newly created thread?
start()
What are the differences between wait() and sleep() methods ?
wait() 1. Releases Lock 2. Method of object class 3. Non static method 4. Notified by notify() or notifyAll() sleep() 1. Doesn't release Lock 2. Method of Thread class 3. Static method 4. After specific amount of time sleep will be completed