File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed
src/main/java/io/ascending/training/concurrency Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments