-1

I have a Theadpool, that creates and makes a group of functions, I would like to stop, pause and resume this group of Theads with buttons, so I can see the results of the simulation. Here is the code:

void mysterious(){

     // 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


}

Help me please , Thanks :)

  • http://stackoverflow.com/questions/9748710 ? – assylias Sep 01 '16 at 17:11
  • Help with what? You posted some code, some requirements; but nothing that would tell us where exactly you are stuck; or what specifically you expect from us. – GhostCat Sep 01 '16 at 17:21
  • 1
    Calling `awaitTermination` on the executor from within a submitted task makes no sense. How do you expect the termination of the executor to complete while one of its jobs is waiting for it? – Holger Sep 01 '16 at 18:06

1 Answers1

2

create a flag called pause in your method, set it as false; in your thread logic, while(pause) thread.sleep(1000L) then you can set the pause flag whenever you want

you can do this better using a notifyAll and wait on the flag too

Sahan Jayasumana
  • 440
  • 6
  • 10