获取套接字数据后调用单独的线程进行进一步处理

时间:2016-10-25 19:43:01

标签: java multithreading sockets

目前我有这个套接字程序,它将从设备接收数据,然后执行一个冗长的过程,例如检查地理围栏和其他相关逻辑,最后将数据保存到数据库中。以下是我的代码的外观

这是启动套接字的代码     public static void main(String [] args){

      new commS();
   }
   commS() { 

   try {
      // setup the connection pool
            HikariConfig  config = new HikariConfig ();
            config.setJdbcUrl("jdbc:mysql://localhost:3306/****"); 
            config.setUsername("*****"); 
            config.setPassword("*****");      
            config.setMaximumPoolSize(20);
            //config.setPartitionCount(1);
            connectionPool = new HikariDataSource(config); // setup the connection pool         
      }
      catch (Exception e) {
            e.printStackTrace(System.out);
      }
      try 
      {
               final ServerSocket serverSocketConn = new ServerSocket(8000);                
               while (true) 
                    {
                        try 
                        {
                                Socket socketConn1 = serverSocketConn.accept();
                                new Thread(new ConnectionHandler(socketConn1)).start();                     
                        }
                        catch(Exception e)
                        {
                            System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
                            e.printStackTrace(System.out);
                        }
                    }
      } 
      catch (Exception e) 
      {
         System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
         e.printStackTrace(System.out);
         //System.exit(0); 
      }

以下是连接处理程序的其余套接字连接代码。

class ConnectionHandler implements Runnable {
      private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }

public void run() { // etc
     w = null;
     BufferedReader r = null;  
     String message="";
     try {
          /// here I read for the data and call the rest of the function etc.
          I would like to split into separate thread? 
     }
     catch (SocketTimeoutException ex)  
          { 
               System.out.println("MyError:SocketTimeoutException has been caught in in the main first try");
               ex.printStackTrace();
          }  
          catch (IOException ex)  
          { 
               System.out.println("MyError:IOException has been caught in in the main first try");
               ex.printStackTrace();
          }  


}

我已经标记了///这里我读取了数据并调用了函数的其余部分等。在该run函数中,我接收数据,验证并调用所有其他各种函数等,最后将数据保存到db中。我觉得这是一个漫长的过程。我想打破它的两个第一部分只是为了读取数据,然后将这个数据串传递给单独的线程,所以我想要实现的是正确的。

所以在run函数中我计划做这个Thread t = new Thread(new MyRProcessing(parameter));    t.start();我不太确定这是对的吗?我应该将MyRProcessing编写为单独的.java还是将所有内容放入一个java中?

public class MyRProcessing implements Runnable {
      private X parameter;
      public MyRProcessing (X parameter) {
         this.parameter = parameter;
      }

      public void run() {
      }
   }

1 个答案:

答案 0 :(得分:1)

您可以找到producer-consumer here

的示例

这涉及multithreadingsynchronization的一般问题,我强烈建议您在继续之前阅读这些问题。

执行您提议的最简单方法是简单地扩展Thread类并通过构造函数传递数据,然后在run()方法中处理它。在大多数情况下,这不是最好的方法,但它很简单。

实施例。

public class MyThread extends Thread {
    private String data;
    public MyThread(String data) {
        this.data = data;
    }

    public void run() { /* process data */ }
}

然后启动线程,一旦完成,它将返回并且线程将终止。