Search code examples
javamultithreadingthread-safetyjung2

How to pause a pre-implemented Runnable class in java?


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;
    }

Solution

  • 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.