Skip to main content

Thread methods

CODE

// WAP to demonstrate use of yield() stop() and sleep() methods.

public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new Thread1());
Thread t2 = new Thread(new Thread2());
t1.start();
t2.start();
}
}

class Thread1 implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thread 1: " + i);
Thread.yield();
}
}
}

class Thread2 implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Thread 2: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}