2014-10-19 72 views
0

我想調用一個函數cycle(),該函數每x秒鐘執行一次操作。
此時我只能在客戶有請求時才能打電話。如何在Runnable中使用Java Socket

public class ChatServer implements Runnable { 

    private static final int PORT = 9001; 
    private static HashSet<String> names = new HashSet<String>(); 
    private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>(); 

    public static void main(String[] args) throws Exception { 
     System.out.println("The chat server is running."); 
     ServerSocket listener = new ServerSocket(PORT); 
     try { 
      while (true) { 
       new Handler(listener.accept()).start(); 
      } 
     } finally { 
      listener.close(); 
     } 
    } 

    private static class Handler extends Thread { 
     private String name; 
     private Socket socket; 
     private BufferedReader in; 
     private PrintWriter out; 

     public Handler(Socket socket) { 
      this.socket = socket; 
     } 

     public void run() { 

      cycle();   

      try { 

       in = new BufferedReader(new InputStreamReader(
        socket.getInputStream())); 
       out = new PrintWriter(socket.getOutputStream(), true); 

       while (true) { 
        out.println("SUBMITNAME"); 
        name = in.readLine(); 
        if (name == null) { 
         return; 
        } 
        synchronized (names) { 
         if (!names.contains(name)) { 
          names.add(name); 
          break; 
         } 
        } 
       } 

       out.println("NAMEACCEPTED"); 
       writers.add(out); 
       while (true) { 
        String input = in.readLine(); 
        if (input == null) { 
         return; 
        } 
        for (PrintWriter writer : writers) { 
         writer.println("MESSAGE " + name + ": " + input); 
        } 
       } 
      } catch (IOException e) { 
       System.out.println(e); 
      } finally { 

       if (name != null) { 
        names.remove(name); 
       } 
       if (out != null) { 
        writers.remove(out); 
       } 
       try { 
        socket.close(); 
       } catch (IOException e) { 
       } 
      } 
     } 
    } 
} 

是的,下面的代碼是工作時,我從我的代碼中刪除Handle類。
我的問題是我不能將它們結合在一起,請幫助或建議。非常感謝。

private void cycle() { 

    //do something 
} 

@Override 
public void run() { 

    long beforeTime, timeDiff, sleep; 

    beforeTime = System.currentTimeMillis(); 

    while (true) { 

     cycle(); 

     timeDiff = System.currentTimeMillis() - beforeTime; 
     sleep = DELAY - timeDiff; 

     if (sleep < 0) { 
      sleep = 2; 
     } 

     try { 
      Thread.sleep(sleep); 
     } catch (InterruptedException e) { 
      System.out.println("Interrupted: " + e.getMessage()); 
     } 

     beforeTime = System.currentTimeMillis(); 
    } 
} 

回答

-1

您是否嘗試在調用偵聽套接字之前爲循環函數啓動一個單獨的線程。

public static void main(String[] args) throws Exception { 
    System.out.println("The chat server is running."); 
    //start separate thread to keep loop.... 
    new Thread(new ChatServer()).start(); 
    ServerSocket listener = new ServerSocket(PORT); 
    try { 
     while (true) { 
      new Handler(listener.accept()).start(); 
     } 
    } finally { 
     listener.close(); 
    } 
} 
+0

這是一個問題,而不是答案。你必須在評論中詢問Marcus。 – Ihsan 2014-10-27 16:07:45