Multithreading
in Java
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.
Multitasking
Multitasking is a process of executing multiple tasks
simultaneously. We use multitasking to utilize the CPU. Multitasking can be
achieved by two ways:
- Process-based
Multitasking(Multiprocessing)
- Thread-based
Multitasking(Multithreading)
1) Process-based Multitasking
(Multiprocessing)
- Each process have its own address in
memory i.e. each process allocates separate memory area.
- Process is heavyweight.
- Cost of communication between the
process is high.
- Switching from one process to another
require some time for saving and loading registers, memory maps, updating
lists etc.
2) Thread-based Multitasking (Multithreading)
- Threads share the same address space.
- Thread is lightweight.
- Cost of communication between the
thread is low.
What is Thread in java
A thread is a lightweight sub process, a 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 shares a common memory area.
Life
cycle of a Thread (Thread States)
1. New
2. Runnable
3. Running
5. Terminated
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.
But for better
understanding the threads, we are explaining it in the 5 states.
The life cycle of the thread in java is
controlled by JVM. The java thread states are as follows:
- New
- Runnable
- Running
- Non-Runnable (Blocked)
- Terminated
1) New
|
2) Runnable
The thread is in runnable state after invocation of start()
method, but the thread scheduler has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has
selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently
not eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method
exits.
How to create thread
There are two ways to create a thread:
- By extending Thread class
- 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.
|
Commonly used Constructors of Thread class:
o Thread()
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
getPriority(): returns the priority of the thread.
7. public int
setPriority(int priority): changes the priority of the
thread.
8. public String
getName(): returns the name of the thread.
9. public void
setName(String name): changes the name of the
thread.
10. public Thread
currentThread(): returns the reference of
currently executing thread.
11. public int
getId(): returns the id of the thread.
12. public
Thread.State getState(): returns the state of the
thread.
13. public boolean
isAlive(): tests if the thread is alive.
14. public void
yield(): causes the currently executing thread object to temporarily
pause and allow other threads to execute.
15. public void
suspend(): is used to suspend the
thread(depricated).
16. public void
resume(): is used to resume the suspended
thread(depricated).
17. public void
stop(): is used to stop the thread(depricated).
18. public boolean
isDaemon(): tests if the thread is a daemon thread.
19. public void
setDaemon(boolean b): marks the thread as daemon or
user thread.
20. public void
interrupt(): interrupts the thread.
21. public boolean
isInterrupted(): tests if the thread has been
interrupted.
22. public static
boolean interrupted(): tests if the current thread
has been interrupted.
|
Starting a thread:
start() method of Thread class is used
to start a newly created thread. It performs following tasks:
o A new
thread starts(with new callstack).
o The
thread moves from New state to the Runnable state.
o When
the thread gets a chance to execute, its target run() method will run.
|
Starting a thread:
start() method of Thread class is used to start a newly created thread. It performs following tasks:
|
1) Java Thread Example by extending Thread class
Output:thread is running...
Output:thread is running...
2) Java Thread Example by implementing Runnable interface
Output:thread is running...
Output:thread is running...
If you are not extending the Thread class,your class object would not be treated as a thread object.So you need to explicitely create Thread class object.We are passing the object of your class that implements Runnable so that your class run() method may execute. |
Sign up here with your email
ConversionConversion EmoticonEmoticon