简体   繁体   English

Scanner.nextInt()阻止

[英]Scanner.nextInt() blocks

My code is working fine if it is the following way. 如果是以下方式,我的代码工作正常。

Scanner input = new Scanner(System.in);
ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i = 0; i < 3; i++) 
{
     Runnable worker = new ClassA();
     executor.execute(worker);
} 

Instead, I want to run continuously in while(true) loop and break the loop when user enters 1. I tried the following way but it is not working. 相反,我想在while(true)循环中连续运行并在用户输入1时中断循环。我尝试了以下方式但它无法正常工作。 it is not running continously but stopping (blocking) at this line int stop = input.nextInt(); 它没有连续运行但在此行停止(阻塞) int stop = input.nextInt(); Can anyone please tell me where the mistake is. 任何人都可以告诉我错误在哪里。 Whether it is the correct way of doing or not if not please suggest an alternative. 是否是正确的做法,如果没有,请建议一个替代方案。 My code in Class A connects to jms queue and should continuously read messages from queue. 我在A类中的代码连接到jms queue ,应该连续读取队列中的消息。

Scanner input = new Scanner(System.in);
ExecutorService executor = Executors.newFixedThreadPool(2);
while(true)
{
     Runnable worker = new ClassA();
     executor.execute(worker);
     int stop = input.nextInt();
     if(stop == 1) {
            break;
     }
 }

EDIT : The following is the edited code. 编辑 :以下是编辑过的代码。 when scanner.nextInt() value is entered 1, finished = true. 当scan.nextInt()值输入1时,finished = true。 While(!finished) loop is break, code in it is not executing but still my Consumer class is running continuously. 虽然(!finished)循环中断,但其中的代码没有执行但仍然是我的Consumer类连续运行。

public class ScannerProblem {
   public static boolean finished = false;
   static class Listener implements Runnable {
      Scanner scanner = new Scanner(System.in);
      @Override
        public void run() {
            while (true) {
                System.out.println("scanning");
                if (scanner.nextInt() == 1) {
                    System.out.println("scanning finished");
                    finished = true;
                    return;
                } else {
                    System.out.println("scanned something strange");
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static class Consumer implements Runnable {
        javax.jms.Connection jmsConnection = null;
        private static ProcessRequest processRequest = new ProcessRequest();
        @Override
        public void run() {
            ActiveMQConnection con = new ActiveMQConnection();
            jmsConnection = con.openTcpConnection();
            try {
                String json = null;
                Session session = jmsConnection.createSession(false,
                        Session.AUTO_ACKNOWLEDGE);
                Queue queue = session.createQueue("Upload_Queue");
                MessageConsumer consumer = session.createConsumer(queue);
                TextMessage message = (TextMessage) consumer.receive();
                System.out.println("Message #" + ": " + message.getText());
                json = message.getText();
                processRequest.upload(json);
                System.out.println("Thread ID: " +                 Thread.currentThread().getId());

            if (jmsConnection != null) {

                    jmsConnection.close();
            }

            Thread.sleep(5000);
        }
            catch(JMSException e) {
                e.printStackTrace();
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            }
    }



    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        pool.submit(new Listener());

        while (!finished) {
            System.out.println("finished::"+finished);
            pool.submit(new Consumer());
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

int stop = input.nextInt() is a blocking call, so it execute ClassA runnable only once then wait for input. int stop = input.nextInt()是一个阻塞调用,因此只执行一次ClassA runnable然后等待输入。

You need to move input code to another thread so it's not blocking the executions of ClassA. 您需要将输入代码移动到另一个线程,因此它不会阻止ClassA的执行。

Maybe a better option would be to have only one execution of ClassA and have another while(true) loop inside it. 也许更好的选择是只有一次执行ClassA并在其中有另一个while(true)循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM