0

I have got the following animator class that implements Runnable (mentioned in JUNG documentation).

How can tell the thread , if some condition was true pause for some time and then start running?

    switch (args[0]) 
    {
        case "agent":
            int size=nodeAttributes.size();
            int i;
            for(i=0;i<size;i++)
            {
                if(args[1].equals(nodeAttributes.get(i).nodeName))
                {
                    VertexCollider vtxCol = new VertexCollider(layout, panel,args[1], args[2] , args[1] , nodeAttributes.get(i));
                    vtxCol.setMaximumIterations(1000);
                    vtxCol.setDesiredPrecision(1);
                    vtxCol.initialize();
                    Animator  animator = new Animator(vtxCol);

                    animator.start();
                    if(nodeAttributes.get(i).isMoving)
                    {
                        animator.stop();
                        animator.wait(10000);
                        System.out.println("stopped");
                    }
                    nodeAttributes.get(i).isMoving = true;
                    break;  
                }
            }
            break;
    }
Freelancer
  • 836
  • 1
  • 14
  • 47

3 Answers3

1

According to the documentation you referenced, an Animator can be paused for a certain amount of time between iteration loops.

animator.setSleepTime(10000);

And then you could set the sleep time to a much shorter interval when your pause condition ended.

However, it seems like you want your Animator to stop entirely when the pause condition is true. In that case, I recommend you stop() the animator at that time (as your code is doing), and then start() it again when the pause condition ends. It appears you can call the stop and start methods repeatedly.

gknicker
  • 5,509
  • 2
  • 25
  • 41
  • The problem is if I `stop` the animator or set the `sleep time` to a long duration ,The program does not return to the function for checking the condition value (And no other function runs after executing this function) and `starting` or changing the `sleep time to shorter interval` Where should I recheck it again? – Freelancer Jan 18 '15 at 05:19
  • Sorry man, I can't solve your design problem using this small amount of information. If you want your animator to wait a while, you can tell it to sleep between loops. If you want to stop and start your animator, you'll have to figure out how to come back and start it. – gknicker Jan 18 '15 at 05:34
  • I have got the complete implementation in the another question if you want to take a look [http://stackoverflow.com/questions/28003792/how-to-pause-jung-animator-given-an-iterativeprocess-by-using-thread-sleep?rq=1] . – Freelancer Jan 18 '15 at 05:40
0

Place wherever you want the program to sleep: (inside an if statement?)

try {
    Thread.sleep(1);
} catch (InterruptedException e) {
    e.printStackTrace();
}

this will tell the thread to sleep however many milliseconds you want it to.

in do-while:

do{

    try {
        Thread.sleep(1);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}while(something() == true);

this will keep on checking the value, until it is false.

EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • I don't want to make the `program` sleep. I just want the `animator` class that implements `runnable` to sleep for some time and then execute. – Freelancer Jan 18 '15 at 05:09
  • Yes the documentation says it is a independent `thread` , and it's also a `protected thread`. – Freelancer Jan 18 '15 at 05:11
  • @AliNfr, then calling `Thread.sleep(milliseconds)` should not hang the entire program, it should only sleep the thread that the animator is on. – EDToaster Jan 18 '15 at 05:13
  • Your solution just has the same problem that I mentioned to @gknicker. – Freelancer Jan 18 '15 at 05:26
  • put this in a do-while loop. – EDToaster Jan 18 '15 at 05:27
  • What do you mean? The `Thread.sleep(milliseconds) ` that you just mentioned can not be used inside the animator class , so it hangs the entire program. – Freelancer Jan 18 '15 at 05:30
  • Because there are other instances of animator and other classes running and I don't want them to be stopped. – Freelancer Jan 18 '15 at 05:39
  • @AliNfr, the Thread.sleep() only hangs the current thread, (which is this animator thread) and the others keep on going. – EDToaster Jan 18 '15 at 05:40
  • The `current thread` is the thread that calls the `animator` class , then the `animator` class itself creates another thread and continues executing. – Freelancer Jan 18 '15 at 05:42
  • Therefore, the inner thread is hung, while the one that calls the class is not. – EDToaster Jan 18 '15 at 05:43
  • Do you mean calling the `Thread.sleep()` inside the animator class? if not the `Thread.sleep()` function sleeps `the calling class` that is not appropriate. – Freelancer Jan 18 '15 at 05:46
  • Oh, you didn't write the animator class? Sorry I thought you meant to write the code INSIDE the animator class. XD oops – EDToaster Jan 18 '15 at 05:58
0

If you want to pause the the animator you must first move the animator object and vtxCol object Initialization to another Thread.Because even if the Thread.sleep() function or wait() function are about to work for your purpose they will stop the function Initializing the animator too which may not fit your needs.

After moving the animator object creation to another separate Thread you must consider creating some LinkedList , Array or some kind of Collection for holding your movement data in order to Initialize the vtxCol and animator object by them , And pass the data to the thread. Then in the Thread.sleep() function can answer your need in that separate thread created.

Freelancer
  • 836
  • 1
  • 14
  • 47