0% found this document useful (0 votes)
7 views

multi threading

Multithreading in Java allows multiple threads to execute simultaneously, enhancing responsiveness and resource sharing. Threads are lightweight processes managed by the scheduler, and they can be implemented using the Runnable interface. Key concepts include thread lifecycle, synchronization, garbage collection, and communication between threads, with distinctions made between processes and threads, as well as methods like wait(), sleep(), notify(), and finalize().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

multi threading

Multithreading in Java allows multiple threads to execute simultaneously, enhancing responsiveness and resource sharing. Threads are lightweight processes managed by the scheduler, and they can be implemented using the Runnable interface. Key concepts include thread lifecycle, synchronization, garbage collection, and communication between threads, with distinctions made between processes and threads, as well as methods like wait(), sleep(), notify(), and finalize().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.What Is Muti threading..?

Multithreading in Java
is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used
to achieve multitasking.
2. benifite of using multi threading in java..?

Responsiveness
Resource Sharing
. Economy
Scalability
Better Communication
3.what is thread in java

A Thread is a very light-weighted process, or we can say the smallest part of the process that allows a program to operate more
efficiently by running multiple tasks simultaneously.

4. what are the ways of implementing thread in java

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.
Runnable interface have only one method named run().
1. public void run(): is used to perform action for a thread.
Starting a thread:
The start() method of Thread class is used to start a newly created thread. It performs the following tasks:
5. difference between thread and process

Process Thread

A process is an instance of a program that is Thread is a segment of a process or a lightweight


being executed or processed. process that is managed by the scheduler
independently.

Processes are independent of each other and Threads are interdependent and share memory.
hence don't share a memory or other resources.

Each process is treated as a new process by the The operating system takes all the user-level threads
operating system. as a single process.

Context switching between two processes takes Context switching between the threads is fast
much time as they are heavy compared to because they are very lightweight.
thread.

The operating system takes more time to Threads can be terminated in very little time.
terminate a process.

New process creation is more time taking as A thread needs less time for creation.
each new process takes all the resources.

6. what is difference between wait() and sleep() method

No Wait() Sleep()
.

1. The Wait() method is related to the Object The Sleep () method is related to the Thread class.
class.

2.

3. It is not a static method. It is a static method.

4. At the time of the Synchronization, the At the time of the Synchronization, the Sleep()
Wait() method releases obj. method doesn't release the obj, i.e., lock.

5. We can call the Wait () method only from We can call the Sleep () method from outside the
the Synchronized context. Synchronized context.

7. difference between notify and notifyall in java

The notify() method 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. The choice is arbitrary and occurs at the discretion of the implementation.
Notifyall()-Wakes up all threads that are waiting on this object's monitor.

8. thread life cycle in java


1) New (Ready to run)
A thread is in New when it gets CPU time.
2) Running
A thread is in a Running state when it is under execution.
3) Suspended
A thread is in the Suspended state when it is temporarily inactive or under execution.

4) Blocked
A thread is in the Blocked state when it is waiting for resources.
5) Terminated
A thread comes in this state when at any given time, it halts its execution immediately.

9.what is difference b/t start and run

start() run()

Creates a new thread and the run() method is executed on the No new thread is created and the run() method is executed on
newly created thread. the calling thread itself.

Can’t be invoked more than one time otherwise


throws java.lang.IllegalStateException Multiple invocation is possible

Defined in java.lang.Runnable interface and must be overriden


Defined in java.lang.Thread class. in the implementing class.
10.what do u mean garbage colletion..
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the
unused objects.

11.what is dead lock and starvation

Deadloc Starvation
k

1 Deadlock is a situation where no process got Starvation is a situation where the low
blocked and no process proceeds priority process got blocked and the high
priority processes proceed.

2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite.

3 Every Deadlock is always a starvation. Every starvation need not be deadlock.

4 The requested resource is blocked by the The requested resource is continuously be


other process. used by the higher priority processes.

5 Deadlock happens when Mutual exclusion, It occurs due to the uncontrolled priority
hold and wait, No preemption and circular wait and resource management.
occurs simultaneously.

12.how to thread communicate to each other..

Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each
other.
Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and
another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following
methods of Object class:

13.purpoese of finalize()
 finalize( ) method is a method of Object class which is called just before the destruction of an object by the garbage collector.
 After finalize( ) method gets executed completely, the object automatically gets destroyed.
 The purpose of calling finalize method is to perform activities related to clean up, resource deallocation etc.
Synchronization in Java
Synchronization in Java is the capability to control the access of multiple threads to any shared resource.
Java Synchronization is better option where we want to allow only one thread to access the shared resource.
Why use Synchronization?
The synchronization is mainly used to
1. To prevent thread interference.
2. To prevent consistency problem.
Types:
1. Process Synchronization
2. Thread Synchronization
14. can you start a thread twice
No. After starting a thread, it can never be started again

what is thread priority in java


In this tutorial, we'll discuss how the Java thread scheduler executes threads on a priority basis.
Additionally, we'll cover the types of thread priorities in Java.

2. Types of Priority
In Java, a thread's priority is an integer in the range 1 to 10. The larger the integer, the higher the priority.
The thread scheduler uses this integer from each thread to determine which one should be allowed to
execute. The Thread class defines three types of priorities:
 Minimum priority
 Normal priority
 Maximum priority
The Thread class defines these priority types as constants MIN_PRIORITY, NORM_PRIORITY,
and MAX_PRIORITY, with values 1, 5, and 10, respectively. NORM_PRIORITY is the default priority for a
new Thread.
what will happen if we don't override thread class run() method
If we don't override run() method, compiler will not flash any error and it will execute run() method of Thread class that has empty implemented ,
So, there will be no output for this thread.
what is the task of main thread in java The Main thread in Java is the one that begins executing when the program starts. All the child
threads are spawned from the Main thread. Also, it is the last thread to finish execution as various shut-down actions are performed by it.

difference type of thread in javsa

You might also like