使用LinkedBlockingQueue挂起

时间:2016-03-29 13:13:09

标签: java multithreading stream

我尝试实现一个可以传递大量字符串的InputStream 从技术上讲,它应该工作,因为我需要做的就是阻止InputStream#read()方法及其相关内容。或者我这么认为...... 这是我对StringInputStream的自定义实现:

public class StringInputStream extends InputStream {
    private LinkedBlockingQueue<Integer> buffer = new LinkedBlockingQueue<>();

    public void supplyData(String s) {
        for (char ch : s.toCharArray()) {
            buffer.add((int) ch);
        }
    }

    @Override
    public int read() throws IOException {
        try {
            return buffer.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return -1;
    }
}

这是我的代码来测试它:

public class StreamsMain {
    public static void main(String[] args) throws InterruptedException {
        InputStream is = new ByteArrayInputStream("eu ma duc la scoala\n sa ma distrez\nsi imi place la MAXIM!".getBytes(StandardCharsets.UTF_8));
        Scanner scanner1 = new Scanner(is);
        AtomicReference<StringInputStream> inputStream = new AtomicReference<>(new StringInputStream());
        Thread th = new Thread(() -> {
            Scanner scanner = new Scanner(inputStream.get());
            while (scanner.hasNextLine()) {
                System.out.println("2. " + scanner.nextLine());
            }
        });
        th.start();

        while (scanner1.hasNextLine()) {
            String line = scanner1.nextLine();
            inputStream.get().supplyData(line + "\n");
            System.out.println("1. " + line);
        }

        System.out.println("\n\nwaiting 3 seconds to exit from MAIN thread");
        TimeUnit.SECONDS.sleep(3);
        //th.interrupt();
        System.out.println("exited MAIN thread");
    }
}

在我的示例中,我正在读取第一个输入流,我将行提供给我的自定义实现,然后我从另一个线程中的自定义实现中读取。

现在奇怪的是: 我没有看到任何输出,除非我退出th.interrupt()行,这只发生在主线程中休眠(因为我从另一个线程中的StringInputStream读取,所以没有意义)。

你能帮我发现问题吗?

亲切的问候,

1 个答案:

答案 0 :(得分:0)

我实际上对此感到好奇,所以我开始查找了

当我尝试

Thread th = new Thread(() -> {
    System.out.println("Before scanner initialization");
    Scanner scanner = new Scanner(inputStream.get());
    while (scanner.hasNextLine()) {
        System.out.println("2. " + scanner.nextLine());
    }
});
th.start();

扫描仪初始化打印前的打印,这意味着新扫描仪的初始化正在阻塞线程。

现在我还没有尝试从InputStream类继承,但是当我尝试在主线程上执行此操作时,初始化没有阻止。

实际上代码阻止

AtomicReference<StringInputStream> inputStream = new AtomicReference<>(new StringInputStream());
System.out.println("before");
Scanner scanner = new Scanner(inputStream.get());
while (scanner.hasNextLine()) { // Code is blocking here
    System.out.println("2. " + scanner.nextLine());
}
System.out.println("after");

所以在打印之前,之后不打印。

想出来

我不确定您的具体要求,但如果您想将Scanner与InputStream一起使用,则应该覆盖

read(byte[] b, int off, int len)

除了read(),因为您正在扩展接口。然而,读取不会从Scanner调用。您可以尝试这个课程,看它是否有效。

class StringInputStream extends InputStream {

    byte[] bytes;
    int index = 0;

    public StringInputStream(byte[] bytes) {
        this.bytes = bytes;
    }

    public int read() {
        return bytes[index];
    }


    public int read(byte[] b, int off, int len) {
        if(index == bytes.length)
            return 0;
        b[0] = bytes[index++];
        return 1;
    }
}

如果从scanner.hasNextLine()返回0个字节,则该类将从read(byte[] b, int off, int len)返回false。猜猜你每天都学到新东西。

希望这有帮助!