What are threads?
Threads are lightweight processes that facilitate in allowing multiple activities in a single process. A thread is a series of executed statements. Each thread has its own program counter, stack and local variables but it shares memory, files and per-process state.
In multithreaded environment, programs make maximum use of CPU so that the idle time can be kept to minimum.
What are the common thread states?
here are several thread states, A thread can be in any one of the state at a particular point of time. It can be running state. It can be ready to run state as soon as it gets CPU time. A running thread can be suspended. A suspended thread can be resumed. A thread can be blocked when waiting for a resource. At any time a thread can be terminated.
Purpose of multithreading?
The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program called a thread. Each thread has a separate path of its execution. So this way a single program can perform two or more tasks simultaneously.
How do you create threads in Java?
A thread can be created in two ways: 1)By extending Thread class 2) By implementing Runnable interface.
Methods to manage threads?
Describe process by creating a thread thru implementing the Runnable interface.
Describe process by creating a thread thru extending Thread class.
What are thread priorities?
What is synchronization?
When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization. To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.
Why use threads?
When does a thread end?
A Thread ends due to the following reasons:
What’s deadlock?
Whenever there is multiple processes contending for exclusive access to multiple locks, there is the possibility of deadlock. A set of processes or threads is said to be deadlocked when each is waiting for an action that only one of the others can perform.
In order to avoid deadlock, one should ensure that when you acquire multiple locks, you always acquire the locks in the same order in all threads.
What are some common guidelines for synchronization?