1) What is Thread?
Ans : Threads are light weight.Creating thread is simple because it requires less resource.
2) What is difference between a Thread and a Process?
Ans :The difference between a Thread and a Process are :
|1. Threads share the address space of the process that created it.||1. Processes have their own address.|
|2. Threads have direct access to the data segment of its process.||2. Processes have their own copy of the data segment of the parent process.|
|3. Threads have almost no overhead.||3. Processes have considerable overhead.|
|4. New threads are easily created.||4. New processes require duplication of the parent process.|
3) What is Thread in java?
Ans :In Java, "thread" means two different things:
- An instance of class java.lang.Thread.
- A thread of execution.
4) What is MultiThreading?
Ans :In Multi-Threaded programming, multiple threads are executing concurrently that improves the performance because CPU is not idle incase some thread is waiting to get some resources. Multiple threads share the heap memory, so it’s good to create multiple threads to execute some task rather than creating multiple processes.
5) What are the two ways of creating Thread?
Ans :The two ways of creating thread are :
- By Extending Thread
- By implementing Runnable interface.
Best way to create thread is by implementing Runnable interface.
6) What are the different states of a Thread's Lifecycle?
Ans : The difference states of a thread's lifecycle are:
- Runnable State
- Running State
- Waited/Blocking/Sleeping State
- Dead State
New : When the instance of thread is created , it will be in new state.
Runnable State :Thread can be in runnable state in 2 ways.
Running State :If thread scheduler allocates CPU then thread will be in running state.
Waited/Blocking/Sleeping State :Threads can be temporarly inactive for a short period of time.
Dead State :Threads dies automatically when thread run method execution is completed and thread object will be garbage collected.
7) What are the two types of MultiTasking?
Ans :The two types of multitasking are:
- Process based multitasking
- Thread based multitasking
8) What is Synchronization?
Ans :Synchronization is the capabilility of control the access of multiple threads to any shared resource.
It is used :
- To prevent thread interference.
- To prevent consistency problem.
9) In how many ways we can do Synchronization?
Ans : There are 2 ways :
- Synchronization Methods
- Synchronization Blocks
10) What is the difference Synchronization Methods and Synchronization Blocks?
Ans :The difference Synchronization Methods and Synchronization Blocks :
Synchronization Methods : If we want a method of object to be accessed by single thread at a time we declare that method with synchronized keyword.
Synchronization Blocks :Synchronizing few lines of code rather than complete method with the help of synchronized keyword.