-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarvationDemo.java
More file actions
48 lines (39 loc) · 1.46 KB
/
Copy pathStarvationDemo.java
File metadata and controls
48 lines (39 loc) · 1.46 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
43
44
45
46
47
48
//copied code from geeks for geeks
// Java program to illustrate Starvation concept
class StarvationDemo extends Thread {
static int threadcount = 1;
public void run()
{
threadcount++;
System.out.println(threadcount + "st Child" +
" Thread execution starts");
System.out.println("Child thread execution completes");
}
public static void main(String[] args)
throws InterruptedException
{
System.out.println("Main thread execution starts");
// Thread priorities are set in a way that thread5
// gets least priority.
StarvationDemo thread1 = new StarvationDemo();
thread1.setPriority(10);
StarvationDemo thread2 = new StarvationDemo();
thread2.setPriority(9);
StarvationDemo thread3 = new StarvationDemo();
thread3.setPriority(8);
StarvationDemo thread4 = new StarvationDemo();
thread4.setPriority(7);
StarvationDemo thread5 = new StarvationDemo();
thread5 .setPriority(6);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
// Here thread5 have to wait beacause of the
// other thread. But after waiting for some
// interval, thread5 will get the chance of
// execution. It is known as Starvation
thread5.start();
System.out.println("Main thread execution completes");
}
}