Skip to content

Commit 1a7d407

Browse files
author
ryo
committed
add stavation
1 parent 485fe2d commit 1a7d407

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.ascending.training.concurrency;
2+
3+
public class AnotherDisplay implements Runnable {
4+
private ThreadStarveDemo td;
5+
AnotherDisplay(ThreadStarveDemo td){
6+
this.td = td;
7+
}
8+
@Override
9+
public void run() {
10+
try {
11+
// introducing some delay
12+
Thread.sleep(5000);
13+
} catch (InterruptedException e) {
14+
// TODO Auto-generated catch block
15+
e.printStackTrace();
16+
}
17+
td.displayValues();
18+
//System.out.println("Calling again");
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.ascending.training.concurrency;
2+
3+
public class Display implements Runnable {
4+
private ThreadStarveDemo td;
5+
Display(ThreadStarveDemo td){
6+
this.td = td;
7+
}
8+
@Override
9+
public void run() {
10+
td.displayValues();
11+
System.out.println("Calling again");
12+
td.displayValues();
13+
System.out.println("Calling again");
14+
td.displayValues();
15+
//System.out.println("Calling again");
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.ascending.training.concurrency;
2+
3+
public class ThreadStarveDemo {
4+
5+
synchronized void displayValues(){
6+
System.out.println("In ThreadStarveDemo For thread " +
7+
Thread.currentThread().getName());
8+
try {
9+
Thread.sleep(1000);
10+
} catch (InterruptedException e) {
11+
// TODO Auto-generated catch block
12+
e.printStackTrace();
13+
}
14+
System.out.println("For thread " + Thread.currentThread().getName());
15+
for(int i = 0; i < 3; i++){
16+
System.out.println("Value of i " + i);
17+
}
18+
}
19+
20+
public static void main(String[] args) {
21+
ThreadStarveDemo td1 = new ThreadStarveDemo();
22+
// Creating 3 threads
23+
Thread thread0 = new Thread(new AnotherDisplay(td1));
24+
Thread thread1 = new Thread(new Display(td1));
25+
Thread thread2 = new Thread(new Display(td1));
26+
27+
// Setting priorities
28+
thread1.setPriority(Thread.MAX_PRIORITY);
29+
thread2.setPriority(Thread.MAX_PRIORITY);
30+
thread0.setPriority(Thread.MIN_PRIORITY);
31+
32+
thread0.start();
33+
thread1.start();
34+
thread2.start();
35+
36+
}
37+
}

0 commit comments

Comments
 (0)