0% found this document useful (0 votes)
3 views30 pages

Thread

Multithreading in Java allows multiple threads to execute simultaneously, sharing memory and saving resources compared to multiprocessing. It offers advantages such as non-blocking user experiences and time efficiency, while threads can be created by extending the Thread class or implementing the Runnable interface. The document also discusses thread states, concurrency problems, and solutions for managing thread execution.

Uploaded by

Ahmed El-harmel
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)
3 views30 pages

Thread

Multithreading in Java allows multiple threads to execute simultaneously, sharing memory and saving resources compared to multiprocessing. It offers advantages such as non-blocking user experiences and time efficiency, while threads can be created by extending the Thread class or implementing the Runnable interface. The document also discusses thread states, concurrency problems, and solutions for managing thread execution.

Uploaded by

Ahmed El-harmel
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/ 30

Multithreading in Java

• Multithreading in Java is a process of executing multiple threads


simultaneously
• threads use a shared memory area.
• They don't allocate separate memory area so saves memory
• context-switching between the threads takes less time than process.
Advantages of Java Multithreading
• 1) It doesn't block the user because threads are independent and you
can perform multiple operations at the same time.
• it doesn't affect other threads if an exception occurs in a single thread.

• 2) You can perform many operations together, so it saves time.


Multitasking
• Multitasking is a process of executing multiple tasks simultaneously.
• We use multitasking to utilize the CPU.
• Multitasking can be achieved in two ways:
• Process-based Multitasking (Multiprocessing)
• Thread-based Multitasking (Multithreading)
Multiprocessing Multithreading
• Each process has an address in • Threads share the same address
memory. In other words, each
process allocates a separate space.
memory area. • A thread is lightweight.
• A process is heavyweight.
• Cost of communication between • Cost of communication between
the process is high. the thread is low.
• Switching from one process to
another requires some time for
saving and loading registers,
memory maps, updating lists, etc.
What is Thread in java
• A thread is a lightweight subprocess, the smallest unit of processing. .
• Threads are independent. If there occurs exception in one thread, it
doesn't affect other threads. It uses a shared memory area.

Thread class
Life cycle of a Thread (Thread States)
• New
• Active
• Blocked / Waiting
• Timed Waiting
• Terminated
• New: Whenever a new thread is created, it is always in the new state.
For a thread in the new state, the code has not been run yet and thus
has not begun its execution.
• Active: When a thread invokes the start() method, it moves from the
new state to the active state. The active state contains two states
within it: one is runnable, and the other is running.
• Runnable: A thread, that is ready to run is then moved to the
runnable state.
• Running: When the thread gets the CPU, it moves from the runnable
to the running state.
• Blocked or Waiting: Whenever a thread is inactive
• For example, a thread (let's say its name is A) may want to print some data
from the printer.
• However, at the same time, the other thread (let's say its name is B) is
using the printer to print some data.
• Therefore, thread A has to wait for thread B to use the printer.
• Thus, thread A is in the blocked state.
• A thread in the blocked state is unable to perform any execution and thus
never consume any cycle of the Central Processing Unit (CPU).
• Hence, we can say that thread A remains idle until the thread scheduler
reactivates thread A, which is in the waiting or blocked state.
• Timed Waiting: Sometimes, waiting for leads to starvation.
• For example, a thread (its name is A) has entered the critical section
of a code and is not willing to leave that critical section. In such a
scenario, another thread (its name is B) has to wait forever, which
leads to starvation.
• To avoid such scenario, a timed waiting state is given to thread B.
Thus, thread lies in the waiting state for a specific span of time, and
not forever.
• A real example of timed waiting is when we invoke the sleep()
method on a specific thread. The sleep() method puts the thread in
the timed wait state.
• Terminated: A thread reaches the termination state because of the
following reasons:
• When a thread has finished its job, then it exists or terminates
normally.
• Abnormal termination: It occurs when some unusual events such as
an unhandled exception or segmentation fault.
• A terminated thread means the thread is no more in the system.
• In other words, the thread is dead, and there is no way one can
respawn (active after kill) the dead thread.
Java Threads | How to create a thread in Java
• There are two ways to create a thread:
• By extending Thread class
• By implementing Runnable interface.
1) Java Thread Example by extending Thread
class
2) Java Thread Example by implementing
Runnable interface
Running Threads
Running Threads
Exercise
• Differences between "extending" and "implementing" Threads?
Concurrency Problems
• Because threads run at the same time as other parts of the program,
there is no way to know in which order the code will run.
• When the threads and main program are reading and writing the
same variables, the values are unpredictable.
• The problems that result from this are called concurrency problems.
one possible solution
• is to use the isAlive() method of the thread to check
whether the thread has finished running before using
any attributes that the thread can change.

You might also like