0% found this document useful (0 votes)
16 views8 pages

Threads

Uploaded by

220701299
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)
16 views8 pages

Threads

Uploaded by

220701299
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/ 8

1.

Thread:

- is a single sequential flow of control within a program.


- is a smallest part of the process that allows a program to operate more efficiently by running
multiple tasks simultaneously.
- It has its own path of execution so each thread of a process is independent.
- lightweight sub-process.
- Threads can be used to perform complicated tasks in the background without interrupting the
main program.
- Every thread has a priority. Threads with higher priority are executed in preference to threads
with lower priority.

2. Multi-threading:

- a process of executing multiple threads simultaneously.


- The ability of a program or an operating system to enable more than one user at a time without
requiring multiple copies of the program running on the computer.
- is a Java feature that allows concurrent execution of two or more parts(threads) of a program for
maximum utilization of CPU.

3. Multi Threads vs Processing


4. Thread Life Cycle
1. New:
- When a thread is created, it is in the "New" state.

- At this point, the thread is not yet scheduled for execution and it’s a state internal to Java programming.

2. Runnable:
- A thread enters the "Runnable" state after invoking the start() method.
- In this state, the Thread is eligible to be scheduled by the operating system and can start executing its
code.
- Whether to run this thread instantly or keep it in runnable thread pool before running, depends on the
OS implementation of thread scheduler

3. Running
- When thread is executing, it’s state is changed to Running.
- Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to
Running.

- Then CPU starts executing this thread

4. Blocked/Waiting:
- A thread can enter the "Blocked" or "Waiting" state under certain circumstances.
- If a thread is waiting for a lock to be released by another thread, it goes into the "Blocked" state.
- Similarly, if a thread waits for a specific condition to be satisfied, it enters the "Waiting" state.

- In these states, the Thread is not actively executing its code and is not eligible for scheduling.

5. Timed Waiting:
- A thread can enter into the "Timed Waiting" state when they are paused for a specified amount of time.
This can happen when using. This can occur if it calls methods sleep() or wait() with a specific timeout
value.
- The Thread remains in this specific state until the timeout expires or until it receives a notification from
another thread.

6. Terminated:
- The final state in the thread lifecycle is the "Terminated" state.
- A thread enters this state when it completes its execution or when an unhandled exception occurs
within the Thread.

- Once a thread is terminated, it cannot transition to any other state.

5. Thread Creation Types

1. Extend Thread class


2. Implement Interface
6. Thread Methods

Methods Action Performed

currentThread() Returns a reference to the currently executing thread object

getId() Returns the identifier of this Thread

getName() Returns this thread’s name

getPriority() Returns this thread’s priority

getState() Returns the state of this thread

interrupt() Interrupts this thread

interrupted() Tests whether the current thread has been interrupted

isAlive() Tests if this thread is alive


Methods Action Performed

isDaemon() Tests if this thread is a daemon thread

isInterrupted() Tests whether this thread has been interrupted

join() Waits for this thread to die

join(long millis) Waits at most millis milliseconds for this thread to die

If this thread was constructed using a separate Runnable run object, then that
run() Runnable object’s run method is called; otherwise, this method does nothing and
returns

setPriority(int
Changes the priority of this thread
newPriority)

Causes the currently executing thread to sleep (temporarily cease execution) for
sleep(long millis) the specified number of milliseconds, subject to the precision and accuracy of
system timers and schedulers

Causes this thread to begin execution; the Java Virtual Machine calls the run
start()
method of this thread

A hint to the scheduler that the current thread is willing to yield its current use of a
yield()
processor

Object wait methods has three variance, one which waits indefinitely for any other
thread to call notify or notifyAll method on the object to wake up the current
Wait()
thread. Other two variances puts the current thread in wait for specific amount of
time before they wake up.

notify method wakes up only one thread waiting on the object and that thread
notify () starts execution. So if there are multiple threads waiting for an object, this method
will wake up only one of them. The choice of the thread to wake depends on the
Methods Action Performed

OS implementation of thread management.

wakes up all the threads waiting on the object, although which one will process
first depends on the OS implementation.
notifyAll() These methods can be used to implement producer consumer problem where
consumer threads are waiting for the objects in Queue and producer threads put
object in queue and notify the waiting threads.

7. Synchronization

 Synchronized method.
 Synchronized block.
 Static synchroniz ation.

8. DeadLock

- a situation where two or more threads are blocked forever, waiting for each other

You might also like