当前位置: 首页>>代码示例>>Java>>正文


Java Stream.close方法代码示例

本文整理汇总了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();
        }
    }
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:27,代码来源:StreamCloseTest.java

示例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();
        }
    }
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:12,代码来源:StreamCloseTest.java

示例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());
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:16,代码来源:WhileOpTest.java

示例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]);
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:50,代码来源:StreamCloseTest.java

示例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]);
}
 
开发者ID:streamsupport,项目名称:streamsupport,代码行数:51,代码来源:StreamCloseTest.java


注:本文中的java8.util.stream.Stream.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。