使用套接字JAVA进行多线程处理

时间:2014-09-26 15:53:15

标签: java multithreading sockets

我遇到多线程问题。我不知道为什么,但线程按顺序运行。 有一个客户端 - 服务器应用程序。我需要运行几个并行线程来进行消息交换。整个课程都太大了,所以我将展示主要部分。

客户代码(每次按下按钮时都会运行此代码):

  public ArrayList<Action> call() {
        Thread myThready = new Thread(new Runnable()
        {
            public void run()
            {
                BufferedReader in;
                PrintWriter out;
                try{
                    Socket fromserver = new Socket(ip, PortID);
                    in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
                    out = new PrintWriter(fromserver.getOutputStream(),true);

                    writeLog(query, ID, 0);
                    out.println(query+"ID"+ID);
                    String fserver = in.readLine();
                    writeLog(fserver, ID, 1);
                    out.println("exit");
                    out.close();
                    in.close();
                    fromserver.close();
                }
                catch (IOException io){
                    return;
                }
            }
        });
        myThready.start();
}

服务器代码:

public void run(){
    flag=true;
    System.out.println("Welcome to Server side!");

    createLog();
    ExecutorService service = Executors.newCachedThreadPool();
    ServerSocket servers = null;
    int n=4600;
    try{
    servers = new ServerSocket(n);
    } catch( Exception e){

    }
    Socket fromclient = null;

    while(true){
        try {
            System.out.print("Waiting for a client...");
            fromclient = servers.accept();
            System.out.println("Client connected.");
            Callable<ArrayList<Action>> callable = new HandleThread(fromclient);
            Future<ArrayList<Action>> future = service.submit(callable);

            list.addAll(future.get());
        } catch (Exception e) {
            System.out.println("Can't accept.");
            System.exit(-1);
        }
    }
}

此代码执行“accept()”,然后为某些计算创建新线程。

HandleThread代码:

public ArrayList<Action> call() {
    BufferedReader in = null;
    PrintWriter out= null;
    try {
        in  = new BufferedReader(new
                InputStreamReader(fromclient.getInputStream()));
        out = new PrintWriter(fromclient.getOutputStream(),true);
        String         input,output;

        System.out.println("Wait for messages.");

        while ((input = in.readLine()) != null) {
            //close filewriter thread if input==exit
            if(input.equalsIgnoreCase("exit")){
                break;
            }
            System.out.println(input);
            String[] arr = input.split("ID");
            System.out.println("+"+arr[0]);
            ID = Integer.parseInt(arr[1]);
            writeLog(arr[0], ID, 0);
            process(arr[0], ID);
            out.println(arr[0]);
            writeLog(arr[0], ID, 1);
        }
        out.close();
        in.close();
        fromclient.close();

    } catch(IOException e){
        return null;
    }
    return list;
}

我不知道为什么这不起作用。我有日志,我看到一个线程只在另一个线程之后运行。不是在同一时间! 拜托,帮助我!

1 个答案:

答案 0 :(得分:2)

Future#get()是一个阻止通话。

list.addAll(future.get());

调用线程将一直等到任务完成。因此,调用accept()的服务器线程在到达下一个任务之前等待每个任务完成。

相关问题