0% found this document useful (0 votes)
5 views3 pages

Program No 10 New

Uploaded by

dreamsapnapbozo
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)
5 views3 pages

Program No 10 New

Uploaded by

dreamsapnapbozo
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/ 3

PROGRAM NO:- 10

AIM:- Write a program in java using thread class to show the use of yield stop
and sleep methods
SOURCE CODE:-
import java.util.*;
import java.lang.*;
import java.io.*;
public class ThreadMethods {
public static void main(String[] args) {
A threadA = new A();
B threadB = new B();
C threadC = new C();
System.out.println("Start Thread A");
threadA.start();
System.out.println("Start Thread B");
threadB.start();
System.out.println("Start Thread C");
threadC.start();
System.out.println("End of Main Thread");
}
}
class A extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
if (i == 1) Thread.yield();
System.out.println("\t From Thread A: i=" + i);
}
System.out.println("Exit from A");
}
}

NAMIT GUPTA,
IT-A,
163
class B extends Thread {
public void run() {
for (int j = 1; j <= 5; j++) {
System.out.println("\t From Thread B: j=" + j);
if (j == 3) {
System.out.println("Stopping Thread B after j=3");
interrupt(); //stop() method has been depriciated in java 1.1, Instead of using stop we
are using interrupt method
}
}
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);
if (k == 1) {
try {
sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread C was interrupted");
}
}
}
System.out.println("Exit from C");
}
}

NAMIT GUPTA,
IT-A,
163
OUTPUT:-

NAMIT GUPTA,
IT-A,
163

You might also like