使用JAVA和JNI维护同步

时间:2015-04-28 16:47:43

标签: java c++ multithreading

我一直在努力完成一个GUI驱动的任务,该任务在需要一段时间计算的线程中启动,并且还使用C ++本机代码在JNI上完成。运行计算密集型任务时,同步会丢失,否则同步就可以了。

有人可以帮助实现时间同步的实现结构吗?我有预感,因为我使用的是JNI。

我的问题的结构如下:

1)选择一个按钮(动画)并触发GUI监听器的事件

2)GUI识别出按下了按钮并启动了以下线程:

3)再次按下按钮后,线程关闭。

private class Animator implements Runnable
{

    public void start()
    {
        if (thread == null)
        {
             thread = new Thread(this, "Animator");
             thread.start();
        }
    }

    public void run()
    {
         while (animate.isSelected())
         {
             updateAnimations();
         }
         resetAnimations();
         thread = null;
    }

    public void updateAnimations()
    {
        double time = scene.getTime(); // A singleton class and scene time
        time += 10;
        scene.update(time); // the scene updates its parameters to time and redraws the GUI.  One of the computations is done over the JNI and takes too long and synchronization is lost (i.e. time = [10 40 80 ...])
    }

    Thread thread;
}

0 个答案:

没有答案