2015-12-05 58 views
1

我在服務器和兩個客戶端之間實現了多線程連接,但是當我啓動我的程序時,服務器啓動了一個無限循環。這是一個問題,因爲我的服務器還實現了一個圖形界面,因爲它被這個循環阻塞,所以無法運行。我想補救,選擇一個不同的條件來停止我的週期,而不是使用while(true)週期。 這裏是我的循環代碼:控制多線程連接的條件

public Socket attendi(){ 
    try { 
     System.out.println("inizializzo il server"); 
     ss = new ServerSocket(PORT); 
     while(true){ 
      System.out.println("server pronto in ascolto"); 
      s = ss.accept(); //s is a global Socket object 
      ThreadServer newConnection = new ThreadServer(); 
      newConnection.connect(s); 
      System.out.println("connessione stabilita"); 
     } 


    } catch (IOException ex) { 
     Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    return s; 
} 

你能推薦一個很好的條件時,有沒有更多的客戶端試圖連接到服務器時將停止循環?

回答

0

首先,你需要擴展Runnable

public class MyRunnable extends Runnable { 

    public void run(){ 
     try { 
      System.out.println("inizializzo il server"); 
      ss = new ServerSocket(PORT); 
      while(true){ 
       System.out.println("server pronto in ascolto"); 
       s = ss.accept(); //s is a global Socket object 
       ThreadServer newConnection = new ThreadServer(); 
       newConnection.connect(s); 
       System.out.println("connessione stabilita"); 

       handleConnection(s); 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

然後實現一個守護線程工廠。守護線程不會阻止終止應用程序:

ThreadFactory factory = new DaemonThreadFactory(); 
Thread thread = factory.newThread(new MyRunnable()); 
thread.start(); 

根據所提供的代碼和說明,它就像你不聽我的:

public class DaemonThreadFactory implements ThreadFactory { 
    private ThreadFactory threadFactory = Executors.defaultThreadFactory(); 

    public Thread newThread(Runnable runnable) { 
     Thread thread = threadFactory.newThread(runnable); 
     thread.setDaemon(true); 
     return thread; 
    } 
} 

然後,使用線程工廠創建線程實際上在一個單獨的線程中運行它。

+0

這樣我的循環會停止,當我關閉我的窗口,對吧?但我的問題是,窗口既不打開 – salvo9415

+0

啊,我的誤解。這是否有助於更多? :) –

+0

也謝謝! – salvo9415

1

啓動一個新線程並將此邏輯放入新線程中,以便您的主線程不會被阻塞。

像這樣,您的主線程將簡單地啓動一個新線程並繼續執行進一步的程序。新線程會做你想做的任何事情(在你的情況下有一些阻塞操作),而不會阻塞主線程。

您可以使用Java的構建ExecutorService爲相同。您不需要返回s,因爲它是全局變量。見下面的代碼:

ExecutorService executorService = Executors.newFixedThreadPool(1); 

executorService.execute(new Runnable() { 
    public void run() { 
     try { 
     System.out.println("inizializzo il server"); 
     ss = new ServerSocket(PORT); 
     while(true){ 
      System.out.println("server pronto in ascolto"); 
      s = ss.accept(); //s is a global Socket object 
      ThreadServer newConnection = new ThreadServer(); 
      newConnection.connect(s); 
      System.out.println("connessione stabilita"); 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    } 
}); 

//Now program execution will continue and the new thread created will execute the logic you have placed in `run` method. 
+0

你能更具體嗎?我在哪裏開始這個新的線程? – salvo9415

+0

這會啓動一個新線程,但當窗口關閉時,仍然可能會出現卡住的線程。 –

+0

它工作正常!謝謝你,兄弟! – salvo9415