141. What is the basic concept of multitasking and it's types?
- Process based multitasking
- Thread based multitasking
- Race condition
- Deadlock condition
- Lock starvation
144. How to use threads in java?
By using Thread class, Java.lang.Thread class is used to construct and access individual thread in a multi threaded application.
145. What are the stages of Thread Lifecycle?
- New : It is created by instantiating the Thread class. Thread th = new Thread(this);
- Runnable: By calling the start method that in turn call run method. th.start();
- Not Runnable: Calling wait or sleep method or caused due to io interrupts. wait(); th.sleep(1000);
- Dead or terminate: After run() method completes execution.
146. What are the two ways of creating threads in java?
Two ways of creating Thread in Java:
- Extending from java.lang.Thread class
- Implementing the Runnable Interface
147. Which is the first thread to be executed in a multithreaded process?
The first thread to be executed in a multi-threaded process is called the main thread. The main thread is created automatically on the startup of Java program execution.
148. What is thread scheduling and what does it determines?
The scheduling can be described as the management of execution of various threads on a single CPU.
- It determines which runnable threads to run
- It can be based on thread priority
- It is part of OS or Java Virtual Machine (JVM)
149. What thread scheduling does java runtime supports?
The Java runtime supports fixed priority scheduling algorithm, which is simple and deterministic.
150. Describe thread policy in java?
- Thread priorities are the integers in the range of 1 to 10 that specify the priority of one thread with respect to the priority of another thread.
- Each Java thread is allocated with a priority number ranging between MIN_PRIORITY and MAX_PRIORITY.
- The JRE chooses the runnable thread with the maximum priority of execution, when it had to choose from a range of threads which are ready to execute.
- The Java Run-time Environment executes threads based on their priority.
- A thread with higher priority runs before threads with low priority.
- At any moment of time, a given thread may give up its right to execute by calling the yield method.
- You can set the thread priority after it is created using the setPriority() method declared in the Thread class.
151. What is synchronization?
Synchronization of threads makes sure that a shared resource can be accessed by only one thread at a time, even if multiple threads what to access the shared resource at any given moment of time.
152. Why synchronization is required?
- Problem: Race condition! Two or more threads want to access the same object and each thread want to modify the state of the object.
- Result: Corrupted object. The solution to this is synchronization.
153. What is monitor?
A monitor is an object that can block threads and notify them when it is available.The monitor controls the way in which synchronized method's access an object or class. To enter an object’s monitor, you need to call a synchronized method.
154. How to acheive synchronization in java?
Synchronization can be achieved in Java using synchronized keyword.