同步多线程服务器

时间:2012-10-21 18:56:19

标签: java multithreading

大家好我已经创建了一个多线程聊天服务器,如下所示:

    public class Main {

    public static ServerSocket server;
    public static Socket connection;
    public static int backLog = 100;
    public static int numberOfConnected;
    public static boolean connected = false;
    public final static int potNumber = 6080;
    public static PrintWriter pw;
    public static Scanner input;
    public static int i = 0;

    public static void main(String[] args) {
        startServer();

    }
    public static void startServer(){
        try {
            server = new ServerSocket(potNumber, backLog);
            waitingForConnection();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void waitingForConnection() {
        connected = false;
        i++;
        while (!connected) {
            try {
                if (connected) {

                }
                connection = server.accept();
                Server s = new Server(connection, pw = new PrintWriter(connection.getOutputStream()), input = new Scanner(connection.getInputStream()));
                s.start();
                numberOfConnected++;
                waitingForConnection();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

这个想法是假设这是一个聊天服务器,所以当一个连接到服务器时,它会启动以下线程:

线程

    public void run(){
    while (connection.isConnected()) {
        if (input.hasNext()) {
            String fullMessage = input.nextLine();
            if (fullMessage.equalsIgnoreCase("Connect")) {
                connectHim();
            }else {
                chatMessage(fullMessage);
            }
        }

        }
    }

private void chatMessage(String fullMessage) {
    String name = fullMessage.substring(0, fullMessage.indexOf(" "));
    String message = fullMessage.substring(fullMessage.indexOf(" "), fullMessage.length());
    pw.println(name+": "+message);
    pw.flush();

}
private void connectHim() {
    String name = input.nextLine();
    pw.println(0);
    pw.flush();
    pw.println(1);
    pw.flush();
    pw.println();
    pw.flush();
    pw.println(name);
    pw.flush();

}

所以我的问题如下:

如果绑定到线程1的用户(这是一个示例)并且绑定到线程2的用户向服务器发送消息,我将如何将该消息发送给绑定在线程1上的用户?

1 个答案:

答案 0 :(得分:1)

其中一个选项是使用HashtableHashMap(仅在使用Collections.synchronizedMap(myMap)时调用Map)。当你开始新的线程时,给他一个唯一的名称(例如用户昵称)并将它放到你的集合key - 线程名称和value - 线程作为对象。

if the user that is bound to thread 1 (this is an example) and the user bound to thread 2 sends a message to the server how will i send that message to the user bound on thread 1?

例如,您有user1user2user3。现在你构建3个线程并将它们放到HashMap,如:

Map<String, Thread> threadMap = new HashMap<String,Thread>();
    threadMap = Collections.synchronizedMap(threadMap);



    YourThread th1 = new YourThread();
            threadMap.put("user1", th);

    YourThread th2 = new YourThread();
            threadMap.put("user2", th);

    YourThread th3 = new YourThread();
            threadMap.put("user3", th);

             ....

    Set<String> userSet = threadMap.keySet();

    Iterator<String> it = userSet.iterator();

    Thread currThread = null;

    while(it.hasNext()){
        String key = it.next();

        currThread = threadMap.get(key);

        // do something with currThread         
    }