如何中断BufferedReader的readLine [英] How to interrupt BufferedReader's readLine

查看:210
本文介绍了如何中断BufferedReader的readLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多个线程中逐行读取套接字的输入。如何中断 readLine()以便我可以优雅地停止它阻塞的线程?

I am trying to read input from a socket line by line in multiple threads. How can I interrupt readLine() so that I can gracefully stop the thread that it's blocking?

编辑(赏金):这可以在不关闭套接字的情况下完成吗?

EDIT (bounty): Can this be done without closing the socket?

推荐答案

关闭中断线程上的套接字。这将导致在被中断的线程上抛出异常。

Close the socket on the interrupting thread. This will cause an exception to be thrown on the interrupted thread.

有关此问题和其他并发问题的更多信息,我强烈推荐Brian Goetz的书Java Concurrency in Practice 。

For more information on this and other concurrency issues, I highly recommend Brian Goetz's book "Java Concurrency in Practice".

这篇关于如何中断BufferedReader的readLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆