Skip to content

Commit 764ff4b

Browse files
Initial commit of guarded suspension design pattern
1 parent cca4760 commit 764ff4b

8 files changed

Lines changed: 219 additions & 0 deletions

File tree

guarded-suspension/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
layout: pattern
3+
title: Guarded Suspension
4+
folder: guarded-suspension
5+
permalink: /patterns/guarded-suspension/
6+
pumlid: RScv3SCm3030LU819FRPXg5fIm552tnYPFiyjRi3RkbAaYkdoQr5JBy369vrxz7oaSv6XmPhL3e6TCaJ0msU-CAoilTToyG8DdKOw5z0GzcAlvNAN_WZSD1brBHHPmxv0000
7+
categories: Concurrency
8+
tags:
9+
- Java
10+
- Difficulty-Beginner
11+
---
12+
13+
## Intent
14+
Use Guareded suspension pattern to handle a situation when you want to execute a method on object which is not in a proper state.
15+
![Guarded Suspension diagram](./etc/guarded-suspension.png)
16+
17+
## 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
21+
7.82 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.13" icons="true" automaticImage="JPEG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
4+
router="FAN">
5+
<class id="1" language="java" name="com.iluwatar.guarded.suspension.GuardedQueue"
6+
project="java-design-patterns_guarded-suspension"
7+
file="/java-design-patterns_guarded-suspension/src/main/java/com/iluwatar/guarded/suspension/GuardedQueue.java"
8+
binary="false" corner="BOTTOM_RIGHT">
9+
<position height="277" width="326" x="301" y="172"/>
10+
<display autosize="false" stereotype="true" package="true" initial-value="false" signature="true"
11+
sort-features="false" accessors="true" visibility="true">
12+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
13+
<operations public="true" package="true" protected="true" private="true" static="true"/>
14+
</display>
15+
</class>
16+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</classifier-display>
21+
<association-display labels="true" multiplicity="true"/>
22+
</class-diagram>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@startuml
2+
package com.iluwatar.guarded.suspension {
3+
class GuardedQueue {
4+
- LOGGER : Logger {static}
5+
- sourceList : Queue<Integer>
6+
+ GuardedQueue()
7+
+ get() : Integer
8+
+ put(e : Integer)
9+
}
10+
}
11+
@enduml

guarded-suspension/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.14.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>guarded-suspension</artifactId>
37+
<dependencies>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
</dependency>
42+
</dependencies>
43+
44+
45+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
package com.iluwatar.guarded.suspension;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import java.util.LinkedList;
29+
import java.util.Queue;
30+
31+
32+
33+
public class GuardedQueue {
34+
private static final Logger LOGGER = LoggerFactory.getLogger(GuardedQueue.class);
35+
private Queue<Integer> sourceList;
36+
37+
public GuardedQueue() {
38+
this.sourceList = new LinkedList<>();
39+
}
40+
41+
/**
42+
* @return last element of a queue if queue is not empty
43+
*/
44+
public synchronized Integer get() {
45+
while (sourceList.isEmpty()) {
46+
try {
47+
LOGGER.info("waiting");
48+
wait();
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
return sourceList.peek();
55+
}
56+
57+
/**
58+
* @param e number which we want to put to our queue
59+
*/
60+
public synchronized void put(Integer e) {
61+
sourceList.add(e);
62+
notify();
63+
LOGGER.info("notifying");
64+
65+
}
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
* <p>
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+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
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+
package com.iluwatar.guarded.suspension;
24+
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
import java.util.concurrent.ExecutorService;
29+
import java.util.concurrent.Executors;
30+
import java.util.concurrent.TimeUnit;
31+
32+
/**
33+
* Created by robertt240 on 1/2/17.
34+
*/
35+
public class GuardedQueueTest {
36+
private volatile Integer value;
37+
38+
@Test
39+
public void testGet() {
40+
GuardedQueue g = new GuardedQueue();
41+
ExecutorService executorService = Executors.newFixedThreadPool(2);
42+
executorService.submit(() -> value = g.get());
43+
executorService.submit(() -> g.put(Integer.valueOf(10)));
44+
executorService.shutdown();
45+
try {
46+
executorService.awaitTermination(30, TimeUnit.SECONDS);
47+
} catch (InterruptedException e) {
48+
e.printStackTrace();
49+
}
50+
Assert.assertEquals(Integer.valueOf(10), value);
51+
}
52+
53+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<module>event-asynchronous</module>
135135
<module>queue-load-leveling</module>
136136
<module>object-mother</module>
137+
<module>guarded-suspension</module>
137138
</modules>
138139

139140
<dependencyManagement>

0 commit comments

Comments
 (0)