-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSleepYieldExample.java
More file actions
42 lines (36 loc) · 1.27 KB
/
SleepYieldExample.java
File metadata and controls
42 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class SleepYieldExample {
public static void main(String[] args) {
// *** Sleep Example *** //
System.out.println(Thread.currentThread().getName() + " is sleeping for 3 seconds ");
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main Thread had enough sleep");
// *** Yield Example *** //
System.out.println("Yield Example Starts");
Thread producer = new Producer();
Thread consumer = new Consumer();
producer.setPriority(Thread.MIN_PRIORITY);
consumer.setPriority(Thread.MAX_PRIORITY);
producer.start();
consumer.start();
}
}
class Producer extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("I am Producer : Producing Item " + i);
Thread.yield();
}
}
}
class Consumer extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("I am Consumer : Consuming Item " + i);
Thread.yield();
}
}
}