Java Pattern: Use Atomic Boolean to Return Single Usage Object
- 时间:2020-09-07 13:13:13
- 分类:网络文摘
- 阅读:132 次
Sometimes, you want the data to be accessed only once. For example, when the HTTP data stream is open for reading, once completed, you don’t want to expose the data again. This is to reduce the risks of leaking data.
It turns out we can easily use the following Design Pattern to achieve this – with the help of the Atomic Boolean types in Java. The Atomic data types are thread safe in Java – allowing multiple threads to access and modifying.
We will have a AtomicBoolean field e.g. pending to indicate whether the data is available for access. Once it has been retrieved, we set it to false via compareAndSet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.helloacm; import java.util.concurrent.atomic.AtomicBoolean; public class Example { // is the data available? private final AtomicBoolean pending = new AtomicBoolean(true); // data field private final Object data = new Object(); public Object get() { // when it is true, return the data and set to false if (pending.compareAndSet(true, false)) { return data; } return null; } } |
package com.helloacm;
import java.util.concurrent.atomic.AtomicBoolean;
public class Example {
// is the data available?
private final AtomicBoolean pending = new AtomicBoolean(true);
// data field
private final Object data = new Object();
public Object get() {
// when it is true, return the data and set to false
if (pending.compareAndSet(true, false)) {
return data;
}
return null;
}
}Let’s unit test the above pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.helloacm; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; public class ExampleTest { // test instance private Example instance; @BeforeEach public void setup() { instance = new Example(); } @Test public void test_get_twice_should_return_null() { assertNotNull(instance.get()); assertNull(instance.get()); } } |
package com.helloacm;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
public class ExampleTest {
// test instance
private Example instance;
@BeforeEach
public void setup() {
instance = new Example();
}
@Test
public void test_get_twice_should_return_null() {
assertNotNull(instance.get());
assertNull(instance.get());
}
}The first time, the data should be available for retrieval, then once it is invoked, the get() will return NULL. This allows data to be only accessed once.
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:这道小学奥数题难倒多数学生,解题关键是比例 分享茄子的家常做法,吃起来不油腻,营养美味又下饭 中华人民共和国慈善法(主席令第四十三号) 中华人民共和国深海海底区域资源勘探开发法(主席令第四十二号) 全国人民代表大会常务委员会关于修改《中华人民共和国人口与计划生育法》的决定(主席令第四十一号) 全国人大常委会关于修改《中华人民共和国高等教育法》的决定(主席令第四十号) 中华人民共和国反家庭暴力法(主席令第三十七号) 全国人大常委会关于修改《中华人民共和国教育法》的决定(主席令第三十九号) 中华人民共和国国家勋章和国家荣誉称号法(主席令第三十八号) 中华人民共和国反恐怖主义法(主席令第三十六号)
- 评论列表
-
- 添加评论