Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将秋千与线(而非工人)连接_Java_Multithreading_Swing_Thread Safety - Fatal编程技术网

Java 将秋千与线(而非工人)连接

Java 将秋千与线(而非工人)连接,java,multithreading,swing,thread-safety,Java,Multithreading,Swing,Thread Safety,我为一个项目构建了一个简单的客户端和服务器,使用一个非常基本的文本界面进行测试和开发(下面的代码演示了用法) 客户端库有一个acceptMessage()方法和一个getMessage()方法,这两个方法基本上分别将消息推入或拉出blockingQueue进行输入和输出(在客户端中,这是通过put()和take()调用实现的)。一个线程阻塞System.in并通过acceptMessage将读取行发送到客户端,而另一个线程阻塞client getMessage()方法并在消息到达时将消息回显到S

我为一个项目构建了一个简单的客户端和服务器,使用一个非常基本的文本界面进行测试和开发(下面的代码演示了用法)

客户端库有一个acceptMessage()方法和一个getMessage()方法,这两个方法基本上分别将消息推入或拉出blockingQueue进行输入和输出(在客户端中,这是通过put()和take()调用实现的)。一个线程阻塞System.in并通过acceptMessage将读取行发送到客户端,而另一个线程阻塞client getMessage()方法并在消息到达时将消息回显到System.out。所有这些都很基本,但工作正常

现在我已经让我的客户机库开始工作了,我正试图找出如何将它集成到使用Swing GUI的应用程序中。到目前为止,我只在Netbeans的界面构建工具中构建了一个简单的表单,其中包含一个文本输入框和一个标签。这样做的目的是让文本输入框取代从system.in读取的内容,并让标签显示否则将写入system.out的内容。到那时,我将在Swing中复制我的简单测试应用程序

据我所知,与Swing GUI直接交互的所有东西都必须在Swing线程中运行,但客户机是作为自己的线程运行的。我不认为从GUI向acceptMessage()发送消息会非常困难(我认为这涉及到设置一个ActionPerformed方法,该方法将读取输入框的内容并在客户端调用acceptMessage(),尽管我仍在努力解决这个问题),但我不知道如何获取响应。我知道,由于线程安全问题,我不能在GUI线程中使用客户机调用功能,此外,客户机库的编写方式使我对其消费类一无所知。客户端实例被简单地传递到消费类中,然后使用acceptMessage()和getMessage()发送和接收消息。它不(也不应该)关心消费类实际上是什么

使用这种架构是否可以轻松地将客户机集成到GUI中?如果是这样,处理客户输入的正确方法是什么?(如我所说,我认为一旦我了解了Swing的这一方面,向客户机输出就不会特别困难了)

我使用Swing的主要动机是a)它似乎是Java中文档记录最好的GUI库,b)Netbeans已经有了使用Swing的工具,c)我有一个严格的截止日期,没有时间切换GUI库并从头开始(这是一个大学项目)。如果我有更多的时间,我可能会研究其他图书馆,但我想他们也都有自己的怪癖

import java.io.*;
import java.util.logging.*;

public class TestApp implements Runnable {

    private Client <String>     client              = null;
    private UpstreamChannel     upstream            = null;
    private DownstreamChannel   downstream          = null;
    private Thread              upstreamThread      = null;
    private Thread              downstreamThread    = null;
    private boolean             ending              = false;

    private class UpstreamChannel implements Runnable {

        private TestApp outer   = null;

        @Override
        public void run () {

            Thread.currentThread ().setName ("TestApp.UpstreamChannel");

            try (BufferedReader inReader = new BufferedReader (new InputStreamReader (System.in))) {
                while (!this.outer.ending) {
                    this.outer.client.acceptMessage (inReader.readLine ());
                }
            } catch (IOException ex) {
                Logger.getLogger (this.getClass ().getName ()).log (Level.SEVERE, ex.getMessage (), ex);
            } finally {
                this.outer.ending   = true;
                this.outer.downstreamThread.interrupt ();
                Thread.currentThread ().interrupt ();
                return;
            }
        }

        public UpstreamChannel (TestApp app) {
            this.outer  = app;
        }
    }

    private class DownstreamChannel implements Runnable {

        private TestApp outer   = null;

        @Override
        public void run () {

            Thread.currentThread ().setName ("TestApp.DownstreamChannel");

            try {
                while (!this.outer.ending) {
                    System.out.println (this.outer.client.getMessage ());
                }
            } catch (InterruptedException ex) {
                Logger.getLogger (this.getClass ().getName ()).log (Level.INFO, ex.getMessage (), ex);
            } finally {
                this.outer.ending   = true;
                this.outer.upstreamThread.interrupt ();
                Thread.currentThread ().interrupt ();
                return;
            }
        }

        public DownstreamChannel (TestApp app) {
            this.outer  = app;
        }
    }

    @Override
    public void run () {
        if ((null == this.upstreamThread) 
        && (null == this.downstreamThread)) {
            this.upstreamThread     = new Thread (this.upstream);
            this.downstreamThread   = new Thread (this.downstream);
            this.upstreamThread.start ();
            this.downstreamThread.start ();

            try {
                this.upstreamThread.join ();
                this.downstreamThread.join ();
            } catch (InterruptedException ex) {
                Logger.getLogger (this.getClass ().getName ()).log (Level.INFO, ex.getMessage (), ex);
            } finally {
                this.upstreamThread.interrupt ();
                this.downstreamThread.interrupt ();
                System.out.println ("Sayonara");
            }
        }
    }

    public TestApp (Client <String> client) {
        this.upstream   = new UpstreamChannel (this);
        this.downstream = new DownstreamChannel (this);
        this.client     = client;

        Logger.getLogger (this.getClass ().getName ()).log (Level.INFO, "Class instantiated");
    }
}

所以你的基本问题是如何

while (!this.outer.ending) {
      System.out.println (this.outer.client.getMessage ());
 }
设置标签上的文本而不是打印到标准输出。您是对的,因为这是一个单独的线程,所以不能直接在GUI小部件上调用方法。但是您可以要求GUI线程分派一些代码,这些代码将在GUI线程中运行

while (!this.outer.ending) {
  final String message = this.outer.client.getMessage (); 

    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
           theLabel.setText(message);      

       }
    });
}
另一种方法可能与在GUI调用中使用事件处理程序一样简单

  this.outer.client.acceptMessage (textBox.getText());

但是,如果acceptMessage也是一个阻塞调用,那么这里也需要一个单独的线程,并让GUI将消息传递给该线程。

因此,您的基本问题是如何获得

while (!this.outer.ending) {
      System.out.println (this.outer.client.getMessage ());
 }
设置标签上的文本而不是打印到标准输出。您是对的,因为这是一个单独的线程,所以不能直接在GUI小部件上调用方法。但是您可以要求GUI线程分派一些代码,这些代码将在GUI线程中运行

while (!this.outer.ending) {
  final String message = this.outer.client.getMessage (); 

    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
           theLabel.setText(message);      

       }
    });
}
另一种方法可能与在GUI调用中使用事件处理程序一样简单

  this.outer.client.acceptMessage (textBox.getText());

但是,如果acceptMessage也是一个阻塞调用,那么这里也需要一个单独的线程,并让GUI将消息传递给该线程。

我个人喜欢在actionPerformed方法中遵循以下内容:

public void actionPerformed( ActionEvent event ) {
    Promise<SomeReturn> promise = server.acceptMessage( message, SomeReturn.class );
    promise.onSuccess( new Callback() {
        public void call( SomeReturn result ) {
            // this is on the swing thread
            someLabel.setText( result.getText() );
        }
    });
}
public void actionPerformed(ActionEvent事件){
Promise Promise=server.acceptMessage(message,SomeReturn.class);
onSuccess(新回调(){
公共作废调用(返回结果){
//这是在摇摆线上
someLabel.setText(result.getText());
}
});
}
这是异步操作的promise类:

public class Promise<T> {

    Callback<T> successCallback;
    Callback<? extends Exception> errorCallback;

    // swing client will register callbacks with these methods
    public onSuccess( Callback<T> callback ) {
        successCallback = callback;
    }

    // swing client will register callbacks with these methods
    public Promise<T> onError( Callback<? extends Exception> callback ) {
        errorCallback = callback;
    }

    // network client thread will call these methods when it gets a response
    public void setResult( final T result ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
               if( successCallback != null ) 
                   successCallback.call( result );
               else 
                   log.warning("Response dropped!  No success callback registered " + result );
            }
        });
    }

    // network client thread will call these methods when it gets a response
    public void setError( final Exception ex ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
               if( errorCallback != null ) 
                   errorCallback.call( ex );
               else 
                   log.error( ex );
            }
        });
    }
}
公共类承诺{
回调成功回调;

回调我个人喜欢在您的actionPerformed方法中遵循以下内容:

public void actionPerformed( ActionEvent event ) {
    Promise<SomeReturn> promise = server.acceptMessage( message, SomeReturn.class );
    promise.onSuccess( new Callback() {
        public void call( SomeReturn result ) {
            // this is on the swing thread
            someLabel.setText( result.getText() );
        }
    });
}
public void actionPerformed(ActionEvent事件){
Promise Promise=server.acceptMessage(message,SomeReturn.class);
onSuccess(新回调(){
公共作废调用(返回结果){
//这是在摇摆线上
someLabel.setText(result.getText());
}
});
}
这是异步操作的promise类:

public class Promise<T> {

    Callback<T> successCallback;
    Callback<? extends Exception> errorCallback;

    // swing client will register callbacks with these methods
    public onSuccess( Callback<T> callback ) {
        successCallback = callback;
    }

    // swing client will register callbacks with these methods
    public Promise<T> onError( Callback<? extends Exception> callback ) {
        errorCallback = callback;
    }

    // network client thread will call these methods when it gets a response
    public void setResult( final T result ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
               if( successCallback != null ) 
                   successCallback.call( result );
               else 
                   log.warning("Response dropped!  No success callback registered " + result );
            }
        });
    }

    // network client thread will call these methods when it gets a response
    public void setError( final Exception ex ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
               if( errorCallback != null ) 
                   errorCallback.call( ex );
               else 
                   log.error( ex );
            }
        });
    }
}
公共类承诺{
回调成功回调;

Callback您将要创建一个InputStream,其中包含用于更新Swing事件线程上GUI的代码。此网站上有这方面的示例。我知道,因为我已经编写了一些。对其进行搜索。谢谢,现在我知道要搜索什么了。我将看一看。我正在考虑将一个简单的工作线程与publish()一起使用但我不认为这是理想的。请参见编辑。您将希望创建一个InputStream,其中包含用于更新Swing事件线程上GUI的代码。此网站上有很多这样的例子。我知道,因为我已经编写了一些。对其进行搜索。谢谢,现在我知道要搜索什么了,我来看看。我正在考虑使用s使用publish()执行工作线程,但我认为这并不理想。请参阅编辑