0

我有以下animator实现 Runnable 的类(在JUNG 文档中提到)。

如何判断thread某个条件是否pause在一段时间内为真然后start运行?

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

3 回答 3

1

根据您引用的文档,Animator可以在迭代循环之间暂停一段时间。

animator.setSleepTime(10000);

然后,当您的暂停条件结束时,您可以将睡眠时间设置为更短的时间间隔。

但是,您似乎希望您的 Animator 在暂停条件为真时完全停止。在这种情况下,我建议您stop()当时的动画师(正如您的代码所做的那样),然后start()在暂停条件结束时再次使用它。看来您可以重复调用停止和启动方法。

于 2015-01-18T05:14:48.157 回答
0

放置在您希望程序休眠的任何位置:(在 if 语句中?)

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

这将告诉线程睡眠,无论你想要多少毫秒。

在做的时候:

do{

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

}while(something() == true);

这将继续检查该值,直到它为假。

于 2015-01-18T05:05:42.100 回答
0

如果要暂停,则animator必须首先将animator对象和vtxCol对象初始化移动到另一个线程。因为即使Thread.sleep()函数或wait()函数即将为您的目的工作,它们也会停止函数初始化,animator这可能不符合您的需要。

将对象创建移动到另一个单独的线程后,animator您必须考虑创建一些或某种用于保存移动数据的对象,以便通过它们初始化对象,并将数据传递给线程。然后在函数中可以在创建的那个单独的线程中回答你的需要。LinkedListArrayCollectionvtxColanimatorThread.sleep()

于 2015-01-20T12:26:47.990 回答