Skip to content

Commit c434049

Browse files
apply changes from review iluwatar#69
1 parent a31962e commit c434049

File tree

7 files changed

+96
-9
lines changed

7 files changed

+96
-9
lines changed

guarded-suspension/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ tags:
1010
---
1111

1212
## Intent
13-
Use Guareded suspension pattern to handle a situation when you want to execute a method on object which is not in a proper state.
13+
Use Guarded suspension pattern to handle a situation when you want to execute a method on object which is not in a proper state.
1414

1515
![Guarded Suspension diagram](./etc/guarded-suspension.png)
1616

1717
## Applicability
18-
Use Guareded Suspension pattern when:
19-
20-
* the developer knows that the method execution will be blocked for a finite period of time
18+
Use Guarded Suspension pattern when the developer knows that the method execution will be blocked for a finite period of time
2119

20+
## Related patterns
21+
* Balking
2.25 KB
Loading

guarded-suspension/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<dependency>
3939
<groupId>junit</groupId>
4040
<artifactId>junit</artifactId>
41+
<scope>test</scope>
4142
</dependency>
4243
</dependencies>
4344
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
/**
24+
* Guarded-suspension is a concurrent design pattern for handling situation when to execute some action we need
25+
* condition to be satisfied.
26+
* <p>
27+
* Implementation is based on GuardedQueue, which has two methods: get and put,
28+
* the condition is that we cannot get from empty queue so when thread attempt
29+
* to break the condition we invoke Object's wait method on him and when other thread put an element
30+
* to the queue he notify the waiting one that now he can get from queue.
31+
*/
32+
package com.iluwatar.guarded.suspension;
33+
34+
import java.util.concurrent.ExecutorService;
35+
import java.util.concurrent.Executors;
36+
import java.util.concurrent.TimeUnit;
37+
38+
/**
39+
* Created by robertt240 on 1/26/17.
40+
*/
41+
public class App {
42+
/**
43+
* Example pattern execution
44+
*
45+
* @param args - command line args
46+
*/
47+
public static void main(String[] args) {
48+
GuardedQueue guardedQueue = new GuardedQueue();
49+
ExecutorService executorService = Executors.newFixedThreadPool(3);
50+
51+
//here we create first thread which is supposed to get from guardedQueue
52+
executorService.execute(() -> {
53+
guardedQueue.get();
54+
}
55+
);
56+
57+
//here we wait two seconds to show that the thread which is trying to get from guardedQueue will be waiting
58+
try {
59+
Thread.sleep(2000);
60+
} catch (InterruptedException e) {
61+
e.printStackTrace();
62+
}
63+
//now we execute second thread which will put number to guardedQueue and notify first thread that it could get
64+
executorService.execute(() -> {
65+
guardedQueue.put(20);
66+
}
67+
);
68+
executorService.shutdown();
69+
try {
70+
executorService.awaitTermination(30, TimeUnit.SECONDS);
71+
} catch (InterruptedException e) {
72+
e.printStackTrace();
73+
}
74+
}
75+
76+
}

guarded-suspension/src/main/java/com/iluwatar/guarded/suspension/GuardedQueue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
public class GuardedQueue {
3333
private static final Logger LOGGER = LoggerFactory.getLogger(GuardedQueue.class);
34-
private Queue<Integer> sourceList;
34+
private final Queue<Integer> sourceList;
3535

3636
public GuardedQueue() {
3737
this.sourceList = new LinkedList<>();
@@ -49,17 +49,17 @@ public synchronized Integer get() {
4949
e.printStackTrace();
5050
}
5151
}
52-
52+
LOGGER.info("getting");
5353
return sourceList.peek();
5454
}
5555

5656
/**
5757
* @param e number which we want to put to our queue
5858
*/
5959
public synchronized void put(Integer e) {
60+
LOGGER.info("putting");
6061
sourceList.add(e);
61-
notify();
6262
LOGGER.info("notifying");
63-
63+
notify();
6464
}
6565
}

guarded-suspension/src/test/java/com/iluwatar/guarded/suspension/GuardedQueueTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ public void testGet() {
4747
Assert.assertEquals(Integer.valueOf(10), value);
4848
}
4949

50+
@Test
51+
public void testPut() {
52+
GuardedQueue g = new GuardedQueue();
53+
g.put(12);
54+
Assert.assertEquals(Integer.valueOf(12), g.get());
55+
56+
}
57+
5058
}

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<module>factory-kit</module>
122122
<module>feature-toggle</module>
123123
<module>value-object</module>
124-
<module>module</module>
124+
<module>module</module>
125125
<module>monad</module>
126126
<module>mute-idiom</module>
127127
<module>mutex</module>
@@ -134,6 +134,8 @@
134134
<module>event-asynchronous</module>
135135
<module>queue-load-leveling</module>
136136
<module>object-mother</module>
137+
<module>guarded-suspension</module>
138+
137139
</modules>
138140

139141
<dependencyManagement>

0 commit comments

Comments
 (0)