本文整理汇总了Java中java8.util.stream.Stream.close方法的典型用法代码示例。如果您正苦于以下问题:Java Stream.close方法的具体用法?Java Stream.close怎么用?Java Stream.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java8.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.close方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConsumed
import java8.util.stream.Stream; //导入方法依赖的package包/类
public void testConsumed() {
Stream<Integer> s1 = StreamSupport.stream(countTo(100));
try {
s1.forEach(i -> {});
// Adding onClose handler when stream is consumed is illegal
// handler must not be registered
checkISE(() -> s1.onClose(() -> fail("s1")));
} finally {
if (s1 != null) {
s1.close();
}
}
// close() must be idempotent:
// second close() invoked at the end of try-with-resources must have no effect
Stream<Integer> s2 = StreamSupport.stream(countTo(100));
try {
s2.close();
// Adding onClose handler when stream is closed is also illegal
checkISE(() -> s2.onClose(() -> fail("s2")));
} finally {
if (s2 != null) {
s2.close();
}
}
}
示例2: testEmptyCloseHandler
import java8.util.stream.Stream; //导入方法依赖的package包/类
public void testEmptyCloseHandler() {
Stream<Integer> ints = null;
try {
ints = StreamSupport.stream(countTo(100));
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
}
示例3: testRefDefaultClose
import java8.util.stream.Stream; //导入方法依赖的package包/类
@Test(groups = { "serialization-hostile" })
public void testRefDefaultClose() {
AtomicBoolean isClosed = new AtomicBoolean();
Stream<Integer> s = RefStreams.of(1, 2, 3).onClose(() -> isClosed.set(true));
Stream<Integer> ds = null;
try {
ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3);
ds.count();
} finally {
if (ds != null) {
ds.close();
}
}
assertTrue(isClosed.get());
}
示例4: testOneCloseHandler
import java8.util.stream.Stream; //导入方法依赖的package包/类
public void testOneCloseHandler() {
final boolean[] holder = new boolean[1];
Runnable closer = () -> { holder[0] = true; };
Stream<Integer> ints = null;
try {
ints = StreamSupport.stream(countTo(100));
ints.onClose(closer);
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0]);
Arrays.fill(holder, false);
ints = StreamSupport.stream(countTo(100)).onClose(closer);
try {
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0]);
Arrays.fill(holder, false);
ints = StreamSupport.stream(countTo(100)).filter(e -> true).onClose(closer);
try {
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0]);
Arrays.fill(holder, false);
ints = StreamSupport.stream(countTo(100)).filter(e -> true).onClose(closer).filter(e -> true);
try {
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0]);
}
示例5: testTwoCloseHandlers
import java8.util.stream.Stream; //导入方法依赖的package包/类
public void testTwoCloseHandlers() {
final boolean[] holder = new boolean[2];
Runnable close1 = () -> { holder[0] = true; };
Runnable close2 = () -> { holder[1] = true; };
Stream<Integer> ints = null;
try {
ints = StreamSupport.stream(countTo(100));
ints.onClose(close1).onClose(close2);
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0] && holder[1]);
Arrays.fill(holder, false);
try {
ints = StreamSupport.stream(countTo(100)).onClose(close1).onClose(close2);
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0] && holder[1]);
Arrays.fill(holder, false);
try {
ints = StreamSupport.stream(countTo(100)).filter(e -> true).onClose(close1).onClose(close2);
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0] && holder[1]);
Arrays.fill(holder, false);
try {
ints = StreamSupport.stream(countTo(100)).filter(e -> true).onClose(close1).onClose(close2).filter(e -> true);
ints.forEach(i -> {});
} finally {
if (ints != null) {
ints.close();
}
}
assertTrue(holder[0] && holder[1]);
}