0% found this document useful (0 votes)
30 views2 pages

Threads and Concurrency

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Threads and Concurrency

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Thread and Concurrency

1. What is Multithreading in java?


Answer: Multithreading is a process of executing multiple threads simultaneously. It is used to
obtain the multitasking. It consumes less memory and gives the fast and efficient performance.
2. What is thread?
Answer: A Thread in java is a lightweight, executable unit of the program. Threads can be created
by extending thread class and implementing the runnable interface.
3. Differentiate between thread and process?
Answer:
- A process is an execution of the program, while a thread is a subset of the process.
- Process have different address space in memory, while thread have shared address space.
- Inter-process communication is slower and expensive than inter-thread communication.
4. What are the advantages of multithreading?
Answer:
- Threads are independent.
- The cost of Communication between the processors is low.
- You can perform many operations together. So, it saves time.
5. How you can create a Thread in java?
Answer: 1. By extending thread class.
2. By implementing runnable interface.
3. By using executors from the java.util.concurrent package.
The Runnable interface is preferred, as it does not require an object to inherit the thread class.
6. What is thread class in java?
Answer: A Thread class is a fundamental class in java is for creating and managing threads. This
class provides methods for creation, start and control.
7. What is start method in java?
Answer: A start method in java is used to start the newly formed threads.
8. What is the ‘Runnable’ interface, and how does it relate to multithreading in Java?
Answer: The runnable interface is an interface used to execute code on concurrent threads. It
defines a single method run(). It is used to represent a task that can be executed on concurrently
within a thread.
9. Explain the lifecycle of a thread in java?
Answer: A thread life cycle consists of the states:
- New: in this state, a thread class object is created using new operator, but it is not alive.
- Runnable: in this state, a thread is ready to run after calling a start() method.
- Running: in this state, a thread schedular picks a thread from the ready state and the thread
is running.
- Blocked/waiting: in this state, the thread is not running, but still alive and waiting for another
thread to finish.
- Dead/terminated: a thread is in terminated or dead state when a run() method exits.
10. What is the ‘start’ method in the ‘Thread’ class, and why is it used to begin thread execution?
Answer: The start() method in a thread class helps us to start the thread’s execution. It schedules
the thread to run its run() method concurrently.
11. What is thread priority, and how is it set in Java?
Answer: A thread priority is value indicating relative importance of the thread. It can be set by
using setPriority() method with values ranging from 1 to 10.
12. Explain the concept of thread synchronization?
Answer: One thread is executed at a time and the remaining threads are in waiting state. This
process is known as thread synchronization. It is essential for thread interference and
inconsistency problems.
13. What are the differences between ‘synchronized’ methods and ‘synchronized’ blocks in Java?
Answer:
14. What is a race condition, and how does it occur in multithreaded programs?
Answer:
15. Explain the ‘wait’ and ‘notify’ methods in Java and their role in thread communication.
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:

You might also like