且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

中断线程以等待用户输入

更新时间:2022-05-10 21:20:26

确定,因此此代码的问题在于,必须在检查循环条件之前执行语句br.readLine().因此,为避免这种情况,我可能会有3个线程.

OK so the problem with this code is the fact that the statement br.readLine() must be executed before the loop conditional is checked. So to avoid this I would have 3 threads potentially.

线程1:

这将是主线程或父线程.在此线程中,您将只数到5,然后中断线程2.

This would be the main or parent thread. In this thread you will merely count to 5 and then interrupt thread 2.

线程2:

在此线程中,您将监视静态变量:count,然后将其打印并在被中断时中断线程3.

In this thread you will monitor the static variable: count,then print it and interrupt thread 3 when interrupted.

线程3:

在这里,您将获得输入并将输入传递到一个函数中,该函数会将其添加到count中.中断时什么都不做.

Here is were you would get input and and pass the input into a function which would add it to count. When interrupted do nothing.

如果您有任何问题,请告诉我.

If you have any questions let me know.