使用Java在线程中侦听输入流

使用Java在线程中侦听输入流,java,multithreading,sockets,vlc,interrupt,Java,Multithreading,Sockets,Vlc,Interrupt,所以在这个阶段,我有一个服务器和一个客户端,其中每个都有输入和输出流 我正在使用VLC包装器实现一个视频流。基本上,当我在客户机上选择一个视频时,它会发送服务器下一步应该接收的对象字符串。(视频文件/评论写入/分级写入)。它会在选择视频时发送所有这三个对象,以便更新流,更新评论框和分级框 现在的问题是,当我需要发送这些头并接收数据时,我必须使用以下方法: replyToServer.writeObject("videoFile"); replyToServer.w

所以在这个阶段,我有一个服务器和一个客户端,其中每个都有输入和输出流

我正在使用VLC包装器实现一个视频流。基本上,当我在客户机上选择一个视频时,它会发送服务器下一步应该接收的对象字符串。(视频文件/评论写入/分级写入)。它会在选择视频时发送所有这三个对象,以便更新流,更新评论框和分级框

现在的问题是,当我需要发送这些头并接收数据时,我必须使用以下方法:

        replyToServer.writeObject("videoFile");
        replyToServer.writeObject(selectedVideo);       

        //get comments
        listening();

        //get rating
        listening();
因此,提前知道您希望得到的答复是很难编码的。此外,如果您希望在客户端工作的任何时候从服务器接收某些内容,但不能使用简单的while循环来检查listening()函数,因为它将阻止所有其他功能,该怎么办

PS.listener的结构如下:

public void listening() throws IOException, ClassNotFoundException {
    String mode = null;
            // blocks the process till gets the output.
        mode =  (String) inputFromServer.readObject();
        System.out.println("Mode Received:" +mode);

    if (mode.equals("videoList")) {
        getListFromSocket();
    } else if [.....]
所以…所以我知道这可以通过使用线程来完成。我还没有用Java编写过很多线程,但是有可能对(字符串)inputFromServer.readObject()进行某种中断吗;或者inputFromServer.available(),那么一旦它看到流中有内容,它就会跳转到listening()函数

更新:

通过使用lxx代码,我注意到我收到的空异常是由于更新了gui。我在该侦听()中有一个函数updateComments;。一切似乎都很好,因为我看到的评论等,但它仍然显示了控制台中的空异常

    public void updateComments(List<Comment> commentList) {



    commentLabel.setText("<html><div style='width:130px; border: 1px solid black; list-style-type: none; text-align:left;'>");
    if (commentList.size() == 0) {

         commentLabel.setText(commentLabel.getText()+"<font style='font-family: Century Gothic, sans-serif;  font-weight:normal; color:#444;  font-size:9px;'>No comments...</font><br>");

    } else {

    for (Comment tempComment : commentList) {

         commentLabel.setText(commentLabel.getText()+"<div style='margin:3px;'><font style='font-family: Century Gothic, sans-serif;  font-size:9px; color:#333;'>"+tempComment.getUser()+":</font><br>");
         commentLabel.setText(commentLabel.getText()+"<font style='font-family: Century Gothic, sans-serif; font-weight:normal; font-size:8px;'>"+tempComment.getText()+"</font><br>");
         commentLabel.setText(commentLabel.getText()+"<font style='font-family: Century Gothic, sans-serif; font-weight:normal; font-size:7px;'><i>"+tempComment.getDate()+"</i></font></div><hr>");
    }


    }
     commentLabel.setText(commentLabel.getText()+"</div></html>");

}
public void updateComments(列表注释列表){
commentLabel.setText(“”);
if(commentList.size()==0){
commentLabel.setText(commentLabel.getText()+“无注释…
”; }否则{ for(注释临时注释:注释列表){ commentLabel.setText(commentLabel.getText()+“”+tempComment.getUser()+“:
”; commentLabel.setText(commentLabel.getText()+“”+tempComment.getText()+“
”; commentLabel.setText(commentLabel.getText()+“”+tempComment.getDate()+“
”; } } commentLabel.setText(commentLabel.getText()+); }
为什么不在线程中放置listening()或调用listening(阻塞操作)的循环:

new Thread(new Runnable() {

    @Override
    public void run() {
        listening();

    }

}).start();

这样,阻塞操作在单独的线程中,主代码继续正常执行。

对于提供的代码,它只侦听一次。所以我尝试添加一个while(true)循环。不是,它显示了很多错误。(线程“AWT-EventQueue-0”java.lang.NullPointerException中的异常)我写过,您可以改为使用循环。异常可能不是因为线程。检查哪个对象为空以及为什么为空。还要检查您没有尝试使用启动线程后直接在线程中初始化的数据。可能是您正在尝试访问尚未初始化的数据。噢。很抱歉我没注意到你写的。我将再次查看代码。