[go: up one dir, main page]

0% found this document useful (0 votes)
7 views6 pages

001 Multithreading

Multithreading in Java allows multiple threads to execute simultaneously, utilizing shared memory for efficiency. Threads can be in various states such as New, Runnable, Blocked, Timed Waiting, and Terminated, with independent execution that prevents exceptions in one thread from affecting others. The document also outlines thread creation, execution, and the importance of thread priority in scheduling.

Uploaded by

shumawakjira26
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)
7 views6 pages

001 Multithreading

Multithreading in Java allows multiple threads to execute simultaneously, utilizing shared memory for efficiency. Threads can be in various states such as New, Runnable, Blocked, Timed Waiting, and Terminated, with independent execution that prevents exceptions in one thread from affecting others. The document also outlines thread creation, execution, and the importance of thread priority in scheduling.

Uploaded by

shumawakjira26
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/ 6

Multithreading

Introduction

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.

However, we use multithreading than multiprocessing because threads use a shared memory
area. They don't allocate separate memory area so saves memory, and context-switching
between the threads takes less time than process.

Java multithreading is mostly used in games, animation ,etc.

What is Thread in java

A thread is a lightweight sub-process, the smallest unit of processing. It is a separate path of


execution. Threads are independent. If there occurs exception in one thread, it doesn't affect
other threads. It uses a shared memory area.

Page 1
Advantage of Java Multithreading

1) It doesn't block the user because threads are independent and you can perform
multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an exception occurs
in a single thread.

Thread States

In Java, a thread always exists in any one of the following states. These states are:

1. New
2. Active/Runnable
3. Blocked/waiting
4. Timed Waiting
5. Terminated

Page 2
1. New Thread: When a new thread is created, it is in the new state. The thread has
not yet started to run when thread is in this state. When a thread lies in the new state,
its code is yet to be run and hasn’t started to execute.
2. Runnable State: A thread that is ready to run is moved to runnable state. In this
state, a thread might actually be running or it might be ready run at any instant of time.It
is the responsibility of the thread scheduler to give the thread, time to run.
A multi-threaded program allocates a fixed amount of time to each individual thread.
Each and every thread runs for a short while and then pauses and relinquishes the CPU
to another thread, so that other threads can get a chance to run. When this happens, all
such threads that are ready to run, waiting for the CPU and the currently running thread
lies in runnable state.
3. Blocked/Waiting state: When a thread is temporarily inactive, then it’s in one ofthe
following states: 1. Blocked
2. Waiting
For example, when a thread is waiting for I/O to complete, it lies in the blocked state. It’s
the responsibility of the thread scheduler to reactivate and schedule a blocked/waiting thread.
A thread in this state cannot continue its execution any further until it is moved to Runnable
state. Any thread in these states does not consume any CPU cycle.

A thread is in the blocked state when it tries to access a protected section of code that is
currently locked by some other thread. When the protected section is unlocked, the schedule
picks one of the threads which is blocked for that section and moves it to the runnable state.
Whereas, a thread is in the waiting state when it waits for another thread on a condition.
When this condition is fulfilled, the scheduler is notified and the waitingthread is moved to
runnable state.

4. Timed Waiting: A thread lies in timed waiting state when it calls a method with a
time out parameter. A thread lies in this state until the timeout is completed or untila
notification is received. For example, when a thread calls sleep or a conditionalwait,
it is moved to a timed waiting state.

Page 3
5. Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.

Thread Priority and thread scheduling


Every Java thread has a priority that helps the operating system determine the order in
which threads are scheduled. Java thread priorities are in the range between
MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default,
every thread is given priority NORM_PRIORITY (a constant of 5).

Threads with higher priority are more important to a program and should be allocated
processor time before lower-priority threads. However, thread priorities cannot guarantee
the order in which threads execute and are very much platform dependent.

Creating and executing thread


Creating a Thread:

 Write a class that extends Thread class or implements Runnable interface this is
available in lang package.
 Write public void run () method in that class. This is the method by default executed
by any thread.
 Create an object to that class.
 Create a thread and attach it to the object.
 Start running the threads.

Example 1: write program to create and run a Thread.

Page 4
Output:

Multi -Tasking Using Threads: In multi -tasking, several


tasks are executed at a time. For this purpose, we need more
than one thread. For example, to perform 2 tasks we can take 2
threads and attach them to the 2 tasks.

Then those tasks are simultaneously


executed by the two threads. Using
more than one thread is called ‘multi-
threading’.

Page 5
Example 2: write program to create more than one Thread.

Output:

Page 6

You might also like