Java

Multithreading in Java

By Nishant S Mevawala
What are Threads?
• A piece of code that run in concurrent with other
threads.
• Each thread is a statically ordered sequence of
instructions.
• Threads are being extensively used express
concurrency on both single and multiprocessors
machines.
• Programming a task having multiple threads of
control – Multithreading or Multithreaded
Programming.
2
Life Cycle of
Thread
Threads
• Java has built in thread support for
Multithreading
• Synchronization
• Thread Scheduling
• Inter-Thread Communication:
currentThread
yield
sleep

start
run
stop

setPriority
getPriority
suspend
resume

• Java Garbage Collector is a low-priority thread
Types of Priority
1. Thread.MIN_PRIORITY - 1
2. Thread.MAX_PRIORITY - 10
3. Thread.NORM_PRIORITY - 5
Born
Stop

Start
Run

Running

Yield

Sleep
Suspended
Wait

Runnable

Stop

Resume

Wait

Notify
Stop

Dead
Ways of Multithreading in Java
•
•

Create a class that extends the Thread class
Create a class that implements the Runnable interface

• 1st Method: Extending the Thread class
class MyThread extends Thread
{
public void run()
{
// thread body of execution

•
•

}
}
Creating thread:
MyThread thr1 = new MyThread();
Start Execution:
thr1.start();
Extends Method example
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("t From Thread A: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("t From Thread B: j= "+j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("t From Thread C: k= "+k);
}
System.out.println("Exit from C");
}
}

class ThreadTest
{
public static void main(String S[])
{
new A().start();
new B().start();
new C().start();
}
}
2nd method: Threads by implementing Runnable
interface
class ClassName implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
• Creating Object:
ClassName myObject = new ClassName();
• Creating Thread Object:
Thread thr1 = new Thread( myObject );
• Start Execution:
thr1.start();
Runnable method Example
class ththead implements Runnable
{
String nm;
Thread t;
public ththead(String st)
{
nm=st;
t=new Thread(this);
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(nm + ":" +i);
}
}
}
class theadrun
{
public static void main(String args[])
{
ththead f1=new ththead("one");
ththead f2=new ththead("Two");
ththead f3=new ththead("Three");
f1.t.start();
f2.t.start();
f3.t.start();
}
}

Java Multi Thead Programming

  • 1.
  • 2.
    What are Threads? •A piece of code that run in concurrent with other threads. • Each thread is a statically ordered sequence of instructions. • Threads are being extensively used express concurrency on both single and multiprocessors machines. • Programming a task having multiple threads of control – Multithreading or Multithreaded Programming. 2
  • 3.
  • 4.
    Threads • Java hasbuilt in thread support for Multithreading • Synchronization • Thread Scheduling • Inter-Thread Communication: currentThread yield sleep start run stop setPriority getPriority suspend resume • Java Garbage Collector is a low-priority thread
  • 5.
    Types of Priority 1.Thread.MIN_PRIORITY - 1 2. Thread.MAX_PRIORITY - 10 3. Thread.NORM_PRIORITY - 5
  • 6.
  • 7.
    Ways of Multithreadingin Java • • Create a class that extends the Thread class Create a class that implements the Runnable interface • 1st Method: Extending the Thread class class MyThread extends Thread { public void run() { // thread body of execution • • } } Creating thread: MyThread thr1 = new MyThread(); Start Execution: thr1.start();
  • 8.
    Extends Method example classA extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("t From Thread A: i= "+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("t From Thread B: j= "+j); } System.out.println("Exit from B"); } }
  • 9.
    class C extendsThread { public void run() { for(int k=1;k<=5;k++) { System.out.println("t From Thread C: k= "+k); } System.out.println("Exit from C"); } } class ThreadTest { public static void main(String S[]) { new A().start(); new B().start(); new C().start(); } }
  • 10.
    2nd method: Threadsby implementing Runnable interface class ClassName implements Runnable { ..... public void run() { // thread body of execution } } • Creating Object: ClassName myObject = new ClassName(); • Creating Thread Object: Thread thr1 = new Thread( myObject ); • Start Execution: thr1.start();
  • 11.
    Runnable method Example classththead implements Runnable { String nm; Thread t; public ththead(String st) { nm=st; t=new Thread(this); } public void run() { for(int i=1;i<=5;i++) { System.out.println(nm + ":" +i); } } }
  • 12.
    class theadrun { public staticvoid main(String args[]) { ththead f1=new ththead("one"); ththead f2=new ththead("Two"); ththead f3=new ththead("Three"); f1.t.start(); f2.t.start(); f3.t.start(); } }