Java 不挂起Swing GUI线程的System.in上的readLine

Java 不挂起Swing GUI线程的System.in上的readLine,java,multithreading,swing,Java,Multithreading,Swing,我有下面的代码,应该将System.in重定向到JTextField。但是每当我尝试newbufferedreader(newinputstreamreader(System.in)).readLine(),Swing GUI挂起。如何在不挂起GUI线程的情况下读取System.in中的行 private static LinkedBlockingQueue<Character> sb = new LinkedBlockingQueue<Character>(); Buf

我有下面的代码,应该将System.in重定向到JTextField。但是每当我尝试
newbufferedreader(newinputstreamreader(System.in)).readLine(),Swing GUI挂起。如何在不挂起GUI线程的情况下读取System.in中的行

private static LinkedBlockingQueue<Character> sb = new LinkedBlockingQueue<Character>();
BufferedInputStream s = new BufferedInputStream(new InputStream() {
    int c = -1;

    @Override
    public int read() throws IOException {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    c = sb.take();
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return c;
    }
});
JTextField t = new JTextField();
    t.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(final KeyEvent e) {
            sb.offer(e.getKeyChar());
            if (e.getKeyChar() == '\n' || e.getKeyChar() == '\r') {
                t.setText("");
            }
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
        }
    });

System.setIn(s);
private static LinkedBlockingQueue sb=new LinkedBlockingQueue();
BufferedInputStream s=新的BufferedInputStream(新的InputStream(){
int c=-1;
@凌驾
public int read()引发IOException{
Thread Thread=新线程(new Runnable(){
@凌驾
公开募捐{
试一试{
c=某人采取行动;
}捕获(中断异常ie){
即printStackTrace();
}
}
});
thread.start();
试一试{
thread.join();
}捕捉(中断异常e){
e、 printStackTrace();
}
返回c;
}
});
JTextField t=新的JTextField();
t、 addKeyListener(新的KeyListener(){
@凌驾
public void keyTyped(最终KeyEvent e){
某人提议(如getKeyChar());
如果(e.getKeyChar()='\n'| | e.getKeyChar()=='\r'){
t、 setText(“”);
}
}
@凌驾
按下公共无效键(KeyEvent arg0){
}
@凌驾
公共无效密钥已释放(KeyEvent arg0){
}
});
系统设置(s);

定义一个回调类,在这里,我使用一个接口,你可以跳过这个阶段

interface Callback {
    void updateText(String s);
}

public class  CallbackImpl implements Callback  {// implements this interface so that the caller can call text.setText(s) to update the text field.

    JTextField text;// This is the filed where you need to update on.

    CallbackImpl(JTextField text){//we need to find a way for CallbackImpl to get access to the JTextFiled instance, say pass the instance in the constructor, this is a way.
     this.text=text;
    }

    void updateText(String s){
         text.setText(s);//updated the text field, this will be call after getting the result from console.
    } 
}
定义一个执行作业的线程,并在作业(从控制台读取)完成后调用回调方法

class MyRunable implements Runnable {

    Callback c; // need a callable instance to update the text filed

    public MyRunable(Callback c) {// pass the callback when init your thread
        this.c = c;
    }

    public void run() {
        String s=// some work reading from System.in
        this.c.updateText(s); // after everything is done, call the callback method to update the text to the JTextField.
    }

}
要使其工作,请在侦听器上启动以下线程:

new Thread(new MyRunable(new CallbackImpl(yourJtextFiled))).start();//start another thread to get the input from console and update it to the text field.

坏主意#1-
t.addKeyListener(新的KeyListener()
使用另一个线程进行回调?或者使用future返回结果?@Jaskey你能演示一下如何操作吗?我需要第三方库吗?@b16db0,无需使用任何第三方库,请检查我的答案,看看我是否理解你的问题。@Jaskey我会检查它。先生,我如何实现this.c.callback(s)方法?text.setText(s);不在EDT中(对于Java7和更高版本),@Jaskey是的,尽管我实际上只是将reads移到System.in的EDT之外。对不起,我以为我已经标记了上面的答案。谢谢你