0% found this document useful (0 votes)
85 views10 pages

Object-Oriented Programming (CS F213) : BITS Pilani

This document discusses important thread methods in Java, including join(), suspend(), and resume(). It provides examples of using the join() method to wait for one thread to finish before continuing execution. Modern approaches for suspending and resuming threads use synchronization and flags rather than the deprecated suspend() and resume() methods. Joining threads allows one to wait until a specified thread terminates before continuing.

Uploaded by

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

Object-Oriented Programming (CS F213) : BITS Pilani

This document discusses important thread methods in Java, including join(), suspend(), and resume(). It provides examples of using the join() method to wait for one thread to finish before continuing execution. Modern approaches for suspending and resuming threads use synchronization and flags rather than the deprecated suspend() and resume() methods. Joining threads allows one to wait until a specified thread terminates before continuing.

Uploaded by

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

Object-Oriented Programming (CS F213)

Module VI: Multithreading in Java


CS F213 RL 14.6: Some Important Thread Methods

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 14.6 : Topics

Role of join() Method?


Suspending and Resuming Threads ?

2 Object-Oriented Programming (CS F213)


Joining Threads via join()
Method
final void join() throws interruptedException
join() method allows the thread to wait until the thread
on which it was called terminates.
Calling thread waits until the specified thread joins.

3 Object-Oriented Programming (CS F213)


join() Method Example
class JoinTest
{
public static void main(String args[])
{
Thread T1 = new Thread()
{
public void run()
{
try
{
for(int i =0; i<2; i++)
{
System.out.println("Hello");
Thread.sleep(100);
}
}
catch(InterruptedException e) {}
}// End of Method
}; // End of class and Statement

4 Object-Oriented Programming (CS F213)


join() Method Example
Thread T2 = new Thread()
{
public void run()
{
try
{
for(int i =0; i<2; i++)
{
System.out.println("Welcome");
Thread.sleep(100);
}
}
catch(InterruptedException e) {}
}// End of Methid
}; // End of class and Statement

T1.start();
T2.start();

for(int i =0; i<2;i++)


System.out.println("Hello Welcome");
System.out.println("Main Method Exits");
}// End of main() Method
} // End of class JoinTest
5 Object-Oriented Programming (CS F213)
join() Method Example
Sample Outputs
Hello Welcome Hello Welcome Hello Welcome
Hello Welcome Hello Welcome Hello Welcome
Welcome Main Method Exits Main Method Exits
Hello Welcome Welcome
Main Method Exits Hello Hello
Welcome Welcome Hello
Hello Hello Welcome

6 Object-Oriented Programming (CS F213)


join() Method Example
class JoinTest
{
public static void main(String args[])
{

Thread T1 = ; // Same statement as used Earlier


Thread T2 = ..,; // Same statement as used Earlier
T1.start();
try
{ <<OUTPUT>>
T1.join(); Hello
}
catch(InterruptedException e) { } Hello
T2.start();
try Welcome
{
T2.join();
Welcome
} Hello Welcome
catch(InterruptedException e) { }
for(int i =0; i<2;i++) Hello Welcome
System.out.println("Hello Welcome");
System.out.println("Main Method Exits"); Main Method Exits
} // End of Method
}//End of class
7 Object-Oriented Programming (CS F213)
Suspending and Resuming
Threads
Earlier Versions of Java supports the following two
methods for suspending and resuming the threads
1. final void suspend(); Suspends the Currently
Running Thread
2. final void resume(); Resumes the Currently
Suspended Thread
However, both suspend() and resume() methods have
been deprecated
Modern Approach for suspending and resuming threads is
via synchronization [ Next slide]

8 Object-Oriented Programming (CS F213)


Modern Approach for Suspending and
Resuming Threads
class SampleThread extends Thread
{
.
boolean suspendFlag=false; // Introduce a boolean type flag variable

// Introduce a suspend method which sets the value of suspendFlag = true


void mySuspend() { suspendFlag = true; }

// Introduce a synchronized resume method which sets the value of suspendFlag = false
synchronized void myResume() { suspendFlag = false; }

public void run()


{
// Check the value of flag at the entry
try
{
synchronized(this) { while(suspendFlag) wait(); }
..
}
catch(InterruptedException e) { }
} // End of run Method
} // end of thread class
9 Object-Oriented Programming (CS F213)
Thank You

10 Object-Oriented Programming (CS F213)

You might also like