Multithreading in Java
Multithreading in Java
MULTITHREADING
INTRODUCTION
MULTI-THREADING .. ?
Multithreading is a conceptual programming concept where
a program (process) is divided into two or more subprograms
(process), which can be implemented at the same time in
parallel.
Each part of such a program is called a thread, and each
thread defines a separate path of execution.
Multithreading enables you to write in a way where multiple
activities can proceed concurrently in the same program.
Thread in java
Creating a Thread
}}
classMultiextendsThread{
publicvoidrun(){
System.out.println("threadisrunning...");
}
publicstaticvoidmain(Stringargs[]){
Multit1=newMulti();
t1.start();
}
}
2. By Implementing Runnable
The steps for creating a thread by using the second mechanism
are:
1.Create a class that implements the interface Runnable and
override run() method:
class MyThread implements Runnable {
...
public void run() {
// thread body of execution
}}
2. Creating Object:
MyThread myObject = new MyThread();
3. Creating Thread Object:
Thread thr1 = new Thread(myObject);
4. Start Execution:
thr1.start();
publicstaticvoidmain(Stringargs[]){
Multi3m1=newMulti3();
Threadt1=newThread(m1);
t1.start();
C O N C LU S I O N
Multithreading is an important feature to java
programing. It has various advantages which include:
Responsiveness
Resource sharing
Economy
Utilization of multiprocessor architectures
It doesn't block the user because threads are
independent and you can perform multiple operations
at same time.
You can perform many operations together so it saves
time.
u
o
Y
k
n
a
Th