Concurrent/Non-blocking console keyboard input

烂漫一生 提交于 2019-12-18 03:39:33

问题


I'm working on a MUD in java. I read player input every tick, but I'm using Scanner which uses blocking operations. I want to have non-blocking input.

I've looked at the nio package which has a Selector class, but I'm not sure how to use it with regard to System.in. I figure I'll definitely need it once I'm running a server, but for now everything is offline.

I've tried extending the main class from Applet and overriding keyDown, but that just meant input was no longer accepted after the first one. Sure, I wasn't blocking anything anymore, but then there was no more input. keyDown never got called again, I guess.

Perhaps threads can be interrupted even when they are executing blocking operations?

Thanks for any insight into this problem.


回答1:


You can't do that with the system console because by now it can't be done in a multi-platform way.

You can use swing window as console or find a JNI based approach but it might not work on some platforms.

You may use JCurses. It might work, it's based on JNI and supports Windows and Linux.




回答2:


keyDown() is deprecated so I'd suggest to use processKeyEvent and a keyListener instead.

Perhaps threads can be interrupted even when they are executing blocking operations?

Yes if you have a reference to the thread object you want to interrupt, you can simply call interrupt() method on that instance. And in the run method of the thread you can handle the interrupted exception. However, this seems a little bit hack-ish. I don't see how this is more helpful than using a simple KeyListener.




回答3:


JLine might fit your bill as well:

http://jline.sourceforge.net/apidocs/src-html/jline/ConsoleReader.html#line.1447




回答4:


I had had to solve similar problem with blocking writing/reading from http. In that particular case I used local buffer and Threads.

Idea is simple, one Thread read from stdin and put content in buffer. Second do same with writing.

And then you use nonblocking queries into your buffer.

Sample code:

class NonBlockingReader implements Runnable{
  Reader in;
  List buffer;
  public void run(){
    String line=null;
    while((line=in.readLine())!=null){
      storeLine(line);
    }
  }
  private synchronized storeLine(String line){
    buffer.add(line);
  }
  public synchronized String getLine(){
    if(list.size()>0)
       return list.removeFirst();
    return null;
  }
}

// .. same for writer, then you jast pass stdin and stdout ...


来源:https://stackoverflow.com/questions/2249680/concurrent-non-blocking-console-keyboard-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!