为什么这两个线程不能一起工作?

时间:2016-08-21 08:57:14

标签: java multithreading

客户端代码

import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.net.*;

public class Ex31dot9Client {
    private Socket socket;
    private PrintWriter request;
    private Scanner response; 
    private Scanner input;

//  private final Object lock = new Object();
    private static Lock lock = new ReentrantLock();
    private static Condition nowRequest = lock.newCondition();
    private static Condition nowResponse = lock.newCondition();

    public static void main(String[] args) {
        new Ex31dot9Client();
    }

    public Ex31dot9Client() {
        try {
            socket = new Socket("localhost", 9999);
            request = new PrintWriter(new DataOutputStream(socket.getOutputStream()));
            response = new Scanner(new DataInputStream(socket.getInputStream()));

            input = new Scanner(System.in);
            PrintWriter output = new PrintWriter(System.out);
            new ClientRequestThread(input, request);
            new ClientResponseThread(response, output);

        } catch (UnknownHostException ex) {
            System.out.println(ex);
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    public void request() {
        lock.lock();
        try {
            System.out.println("ME: ");
            String msg = input.nextLine(); 
            //while (!(msg = input.nextLine()).isEmpty()) {
            request.println(msg);
            request.flush();
            //} 
            nowRequest.await();

            nowResponse.signal();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void response() {
        lock.lock();
        try {
            String newMsg; 
            while (!(newMsg = response.nextLine()).isEmpty()) {
                System.out.println(newMsg);
            }
            nowResponse.await();

            nowRequest.signal();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    class ClientRequestThread extends Thread {
        Scanner input;
        PrintWriter request;
        public ClientRequestThread(Scanner input, PrintWriter request) {
            this.input = input;
            this.request = request;
            start();
        }

        @Override
        public void run() {
            while (true) {
                request();
            }
        }
    }

    class ClientResponseThread extends Thread {
        private Scanner response;
        private PrintWriter output;

        public ClientResponseThread(Scanner response, PrintWriter output) {
            this.response = response;
            this.output = output;
            start();
        }

        @Override
        public void run() {
            while (true) {
                response();
            }
        }
    }

}

服务器代码

import java.io.*;
import java.util.*;
import java.net.*;

public class Ex31dot9Server {
    public static void main(String[] args) {
        new Ex31dot9Server();
    }

    private Hashtable<Socket, PrintWriter> outputStreams = new Hashtable<Socket, PrintWriter>();

    public Ex31dot9Server() {
        try {
            ServerSocket ss = new ServerSocket(9999);
            System.out.println("MultiThreadServer started at "+new Date());

            while (true) {
                Socket socket = ss.accept();
                InetAddress ia = socket.getInetAddress();
                System.out.println("Client "+ia.getHostName()+"/"+ia.getHostAddress()+" connected at "+new Date());
                PrintWriter output = new PrintWriter(new DataOutputStream(socket.getOutputStream()));

                outputStreams.put(socket, output);

                new ServerThread(this, socket);     
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }

    }

    public void sendToAll(String msg) {
        for (Enumeration e = outputStreams.elements(); e.hasMoreElements();) {
            PrintWriter output = (PrintWriter)e.nextElement();
            System.out.println("Send msg to "+output);
            output.println(msg);
        }
    }

    class ServerThread extends Thread {
        private Ex31dot9Server server;
        private Socket socket;

        public ServerThread(Ex31dot9Server server, Socket socket) {
            this.server = server;
            this.socket = socket;
            start();
        }

        @Override
        public void run() {
            try {
                Scanner input = new Scanner(new DataInputStream(socket.getInputStream()));
                while (true) {
                    String msg = input.nextLine();
                    System.out.println("receive msg: "+msg);
                    server.sendToAll(msg);
                }
            } catch (IOException ex) {
                System.err.println(ex);
            }
        }
    }
}

我在客户端创建了两个线程。我想从system.in输入一条消息,然后将消息从响应打印到system.out,但我不能。
我做错了什么?

0 个答案:

没有答案