2016-09-01 18 views
-1

私はTheadpoolを作成してグループを作ります。このグループをTheadsのボタンで停止、一時停止、再開したいので、シミュレーションの結果を見ることができます。ここでは、コードは次のようになります。スレッドプールでの作業、すべてのスレッドの一時停止、再生、再開、および停止方法

無効神秘(){

 // create a pool of threads, 10 max jobs will execute in parallel 


    /// start 



    ExecutorService threadPool = Executors.newFixedThreadPool(network.mobiles.size()); 
    // submit jobs to be executing by the pool 

    for (int i=0; i<network.mobiles.size(); i++) { 
    // for (int i=0; i<1; i++) { 

      final int j = i ; 
     threadPool.submit(new Runnable() { 

      public void run() { 

       int value = 100 ; 

       for (int z=0; z<value; z++){ 
        value++; 
        int x = (mobnod[j].getLocation().x) +7; 
        int y = (mobnod[j].getLocation().y) +7 ; 
        System.out.println("x="+x+" , y="+y); 
        double r = Math.random(); 
        System.out.println(r); 
        if (r<0.25) x=x-5; 
        if (r>=0.25 && r<0.50) x=x+5; 
        if (r>=0.5 && r<0.75) y=y-5; 
        if (r>=0.75 && r<1.00) y=y+5; 
        System.out.println("x'="+x+" , y'="+y); 

        if ((0 < x && x<1300) || (0 <y && y <800)){ 

         mobnod[j].setLocation(x-7, y-7); 
         network.mobiles.get(j).findClosestAP(network.nodes); 
         network.setConnections(); 
        } 
        try { 
         threadPool.awaitTermination(1000, TimeUnit.MILLISECONDS); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 

     }); 
    } 
    // once you've submitted your last job to the service it should be shut down 
    threadPool.shutdown(); 
    // wait for the threads to finish if necessary 


} 

は私を助けてください、ありがとうございます:)

+0

HTTPフラグに待つことができ、一時停止フラグを設定することができます://stackoverflow.com/questions/9748710? – assylias

+0

何をお手伝いしますか?あなたはいくつかのコード、いくつかの要件を掲示しました。しかし、あなたがどこにこだわっているのかは分かりません。あなたが私たちに期待していること – GhostCat

+1

送信されたタスクの中からエグゼキュータ上で 'awaitTermination'を呼び出すことは意味がありません。あなたの仕事の1つがそれを待っている間にエグゼクティブの終了がどのように完了すると思いますか? – Holger

答えて

1

それはfalseとして設定し、あなたの方法で一時停止と呼ばれる旗を作成します。あなたのスレッドのロジックで 、 中(一時停止)のThread.sleep(1000L) その後、あなたは

をしたいときにいつでものnotifyAllを使用してこの優れた操作を行うと、あまりにも

関連する問題