Multithreading Bca
Multithreading Bca
4
A Multithreaded Program
Main Thread
(Main method)
start
start start
16
Creating thread using Thread class
• One way of creating thread is to extend the Thread class. The steps to
create a thread using thread class is as follows:
• 1.Derive a subclass from class Thread.
• 2.Override the run( ) method in the class.
• Syntax:
• class classname extends Thread
{
-------
-------
public void run()
{
//statements
}
}
• 3. Create an instance of the subclass.
• 4. Call the start( ) method of class Thread. The start( ) calls run() method.
17
1st method: Extending Thread class
Threads are implemented as objects that contains a method
called run()
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
18
An example
class MyThread extends Thread { // the thread
public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
20
An example
class MyThread implements Runnable {
public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx2 {
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
// due to implementing the Runnable interface
// I can call start(), and this will call run().
t.start();
} // end main()
} // end class ThreadEx2
21
A Program with Three Java Threads
• Write a program that creates 3 threads
22
Three threads example
class A extends Thread
{ public void run()
{for(int i=1;i<=5;i++)
{ System.out.println("\t From ThreadA: i= "+i);
}
System.out.println("Exit from A"); }}
23
class C extends Thread
{ public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t From ThreadC: k= "+k);
}
B b=new B();
b.start();
C c=new C();
c.start();
}}
24
Run 1
java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
From ThreadA: i= 5
Exit from A
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
25
Run2
java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
From ThreadA: i= 5
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
Exit from A
26
Exercise
• WAP that makes use of 3 threads.
• Thread A : print no.s from 1 to 5
• Thread B : print no.s from 6 to 10
• Thread C : print no.s from 11 to 15
Sleep Method
• The sleep() method of Thread class is used to sleep a thread
for the specified amount of time.
catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
} Output:
} 11223344
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
t2.start();
}
}
Thread Priority
• In Java, each thread is assigned priority, which affects
the order in which it is scheduled for running. The
threads so far had same default priority
(NORM_PRIORITY) and they are served using FCFS
policy.
– Java allows users to change priority:
• ThreadName.setPriority(intNumber)
– MIN_PRIORITY = 1
– NORM_PRIORITY=5
– MAX_PRIORITY=10
30
Thread Priorities
for(int i=1;i<=4;i++)
{
System.out.println("\t From ThreadA: i= "+i);
}
33
output
Thread Class and the Runnable Interface
40
An example
class MyThread implements Runnable {
public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx2 {
public static void main(String [] args ) {
Mythread ob=new Mythread();
Thread t = new Thread(ob);
// due to implementing the Runnable interface
// I can call start(), and this will call run().
t.start();
} // end main()
} // end class ThreadEx2
41
Exercise
• WAP that creates two threads:
• A: prints table of 4
• B: prints table of 11
• Use Runnable Interface.
The join() method