|
1 |
| -import java.util.concurrent.ExecutorService; |
2 |
| -import java.util.concurrent.Executors; |
| 1 | +import java.util.concurrent.ExecutorService; |
| 2 | +import java.util.concurrent.Executors; |
3 | 3 |
|
4 |
| -/** |
5 |
| -* @author evivehealth on 08/02/19. |
| 4 | +/** |
| 5 | +* @author evivehealth on 08/02/19. |
6 | 6 | */
|
7 |
| -// Java program depicting |
8 |
| -// concurrent programming in action. |
| 7 | +// Java program depicting |
| 8 | +// concurrent programming in action. |
9 | 9 |
|
10 |
| -// Runnable Class that defines the logic |
11 |
| -// of run method of runnable interface |
12 |
| -public class Counter implements Runnable |
13 |
| -{ |
14 |
| -private final MainApp mainApp; |
15 |
| -private final int loopLimit; |
16 |
| -private final String task; |
| 10 | +// Runnable Class that defines the logic |
| 11 | +// of run method of runnable interface |
| 12 | +public class Counter implements Runnable |
| 13 | +{ |
| 14 | +private final MainApp mainApp; |
| 15 | +private final int loopLimit; |
| 16 | +private final String task; |
17 | 17 |
|
18 |
| -// Constructor to get a reference to the main class |
19 |
| -public Counter |
20 |
| -(MainApp mainApp, int loopLimit, String task) |
21 |
| -{ |
22 |
| -this.mainApp = mainApp; |
23 |
| -this.loopLimit = loopLimit; |
24 |
| -this.task = task; |
25 |
| -} |
| 18 | +// Constructor to get a reference to the main class |
| 19 | +public Counter |
| 20 | +(MainApp mainApp, int loopLimit, String task) |
| 21 | +{ |
| 22 | +this.mainApp = mainApp; |
| 23 | +this.loopLimit = loopLimit; |
| 24 | +this.task = task; |
| 25 | +} |
26 | 26 |
|
27 |
| -// Prints the thread name, task number and |
28 |
| -// the value of counter |
29 |
| -// Calls pause method to allow multithreading to occur |
| 27 | +// Prints the thread name, task number and |
| 28 | +// the value of counter |
| 29 | +// Calls pause method to allow multithreading to occur |
30 | 30 | @Override
|
31 |
| -public void run() |
32 |
| -{ |
33 |
| -for (int i = 0; i < loopLimit; i++) |
34 |
| -{ |
35 |
| -System.out.println("Thread: " + |
| 31 | +public void run() |
| 32 | +{ |
| 33 | +for (int i = 0; i < loopLimit; i++) |
| 34 | +{ |
| 35 | +System.out.println("Thread: " + |
36 | 36 | Thread.currentThread().getName() + " Counter: "
|
37 |
| -+ (i + 1) + " Task: " + task); |
38 |
| -mainApp.pause(Math.random()); |
39 |
| -} |
40 |
| -} |
41 |
| -} |
42 |
| -class MainApp |
43 |
| -{ |
| 37 | ++ (i + 1) + " Task: " + task); |
| 38 | +mainApp.pause(Math.random()); |
| 39 | +} |
| 40 | +} |
| 41 | +} |
| 42 | +class MainApp |
| 43 | +{ |
44 | 44 |
|
45 |
| -// Starts the threads. Pool size 2 means at any time |
46 |
| -// there can only be two simultaneous threads |
47 |
| -public void startThread() |
48 |
| -{ |
49 |
| -ExecutorService taskList = |
50 |
| -Executors.newFixedThreadPool(2); |
51 |
| -for (int i = 0; i < 5; i++) |
52 |
| -{ |
53 |
| -// Makes tasks available for execution. |
54 |
| -// At the appropriate time, calls run |
55 |
| -// method of runnable interface |
56 |
| -taskList.execute(new Counter(this, i + 1, |
57 |
| -"task " + (i + 1))); |
58 |
| -} |
| 45 | +// Starts the threads. Pool size 2 means at any time |
| 46 | +// there can only be two simultaneous threads |
| 47 | +public void startThread() |
| 48 | +{ |
| 49 | +ExecutorService taskList = |
| 50 | +Executors.newFixedThreadPool(2); |
| 51 | +for (int i = 0; i < 5; i++) |
| 52 | +{ |
| 53 | +// Makes tasks available for execution. |
| 54 | +// At the appropriate time, calls run |
| 55 | +// method of runnable interface |
| 56 | +taskList.execute(new Counter(this, i + 1, |
| 57 | +"task " + (i + 1))); |
| 58 | +} |
59 | 59 |
|
60 |
| -// Shuts the thread that's watching to see if |
61 |
| -// you have added new tasks. |
62 |
| -taskList.shutdown(); |
63 |
| -} |
| 60 | +// Shuts the thread that's watching to see if |
| 61 | +// you have added new tasks. |
| 62 | +taskList.shutdown(); |
| 63 | +} |
64 | 64 |
|
65 |
| -// Pauses execution for a moment |
66 |
| -// so that system switches back and forth |
67 |
| -public void pause(double seconds) |
68 |
| -{ |
| 65 | +// Pauses execution for a moment |
| 66 | +// so that system switches back and forth |
| 67 | +public void pause(double seconds) |
| 68 | +{ |
69 | 69 | try
|
70 |
| -{ |
71 |
| -Thread.sleep(Math.round(1000.0 * seconds)); |
72 |
| -} |
73 |
| -catch (InterruptedException e) |
74 |
| -{ |
75 |
| -e.printStackTrace(); |
76 |
| -} |
77 |
| -} |
| 70 | +{ |
| 71 | +Thread.sleep(Math.round(1000.0 * seconds)); |
| 72 | +} |
| 73 | +catch (InterruptedException e) |
| 74 | +{ |
| 75 | +e.printStackTrace(); |
| 76 | +} |
| 77 | +} |
78 | 78 |
|
79 |
| -// Driver method |
80 |
| -public static void main(String[] args) |
81 |
| -{ |
82 |
| -new MainApp().startThread(); |
83 |
| -} |
84 |
| -} |
| 79 | +// Driver method |
| 80 | +public static void main(String[] args) |
| 81 | +{ |
| 82 | +new MainApp().startThread(); |
| 83 | +} |
| 84 | +} |
0 commit comments