0% found this document useful (0 votes)
8 views11 pages

Multithreading

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

Multithreading

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

Multithreading

S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for
Women,Karur.
• Multithreading in java is a process of executing
multiple threads simultaneously.
• Thread is basically a lightweight sub-process, a
smallest unit of processing. Multiprocessing and
• multithreading, both are used to achieve multitasking.
• But we use multithreading than multiprocessing
because threads share a common 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.
Advantages of Java Multithreading
1) It doesn't block the user because threads are
independent and you can perform multiple
operations at 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 exception occur in a single
thread.
Life cycle of a Thread (Thread States)

• A thread can be in one of the five states.


According to sun, there is only 4 states in
thread life
• cycle in java new, runnable, non-runnable
and terminated. There is no running state.
The life cycle of the thread in java is controlled
by JVM. The java thread states are as follows:
• 1. New
• 2. Runnable
• 3. Running
• 4. Non-Runnable (Blocked)
• 5. Terminated
How to create thread
• There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
Thread class:
• Thread class provide constructors and
methods to create and perform operations on
a
• thread.Thread class extends Object class and
implements Runnable interface
• used Constructors of Thread class
oThread()
o Thread(String name)
o Thread(Runnable r)
o Thread(Runnable r,String name)
Commonly used methods of Thread class:
1. public void run(): is used to perform action for a
thread.
2. public void start(): starts the execution of the
thread.JVM calls the run() method on the thread.
3. public void sleep(long miliseconds): Causes the
currently executing thread to sleep (temporarily
cease execution) for the specified number of milliseconds.
4. public void join(): waits for a thread to die.
5. public void join(long miliseconds): waits for a thread to
die for the specified miliseconds.
6. public int setPriority(int priority): changes the priority
of the thread.

You might also like