Java线程未在单独的对象上运行

时间:2015-01-31 11:11:43

标签: java multithreading swing animation

我正在研究一个学校项目,该项目涉及生成两个单独的Swing Canvas对象,这些对象可以在我设计的矩阵/网格数据结构的克隆副本上设置广度/深度优先搜索算法。

我创建了一些类来帮助将Matrix / Grid转换为图形,这些图形组合在一个SearchAnimation类中,该类用作ViewController来管理动画。在下图中,它们显示在右侧(不在黄色背景区域中)。每个SearchAnimation对象都包含JLabel,Canvas和White Background。

以下是布局的屏幕截图: enter image description here

JFrame在应用程序控制器类(ICL.java)中包含两个SearchAnimation类实例。这些动画必须同时运行。我为每个动画创建了单独的Threads,并将它传递给单独的SearchAnimation对象:

public void setupDepthFirstPanel() {
            // Create a new GridGraphic Panel
            //canvasDepthFirst = new GridPanel(null, DEPTH_FIRST_LABEL);
            mDepthAnimation = new SearchAnimation(null, SearchAnimationType.DEPTH_FIRST_ANIMATION);
            mDepthThread = new Thread(mDepthAnimation, "Depth Thread");
        }

        public void setupBreadthFirstPanel() {
            // Create a new GridGraphic Panel
            //canvasBreadthFirst = new GridPanel(null, BREADTH_FIRST_LABEL);
            mBreadthAnimation = new SearchAnimation(null, SearchAnimationType.BREADTH_FIRST_ANIMATION);
            mBreadthThread = new Thread(mBreadthAnimation, "Breadth Thread");
        }

我在ActionListener类中启动线程,该类响应标记为"标签组件"的按钮的Click事件:

if ( source == labelComponents ) {
            if (DEBUG && DEBUG_CLICK_LISTENER) System.out.println("\"Label Components\" Button Clicked!");
            /*This is where the call for the labelBreadth and labelDepth of the
            ICLLogic class is going to occur*/

            // Run Animation
            // Set Up Threads
            System.out.println("ICL.ActionPerformed - Current Thread: " + Thread.currentThread());
            //mBreadthThread = new Thread(mBreadthAnimation, "Breadth Animation");
            //mDepthThread = new Thread(mDepthAnimation, "Depth Animation");

            // Start Threads
            mBreadthThread.start();
            mDepthThread.start();
        }

当程序运行时,"标签组件"单击按钮,只有一个图形开始动画,但似乎两个SearchAnimation线程都在单个JPanel / Canvas中运行,因为动画不遵循任一算法的逻辑。

以下是SearchAnimation中Runnable接口的实现:

// THREAD METHODS
    /** Implementation of the Runnable interface for Multithreading of the
     *  SearchAnimation Class, which allows multiple SearchAnimations to run Concurrently.
     *  In this case, the Breadth & Depth-First SearchAnimations
     * 
     */
    public void run() {
        System.out.println("Thread Started - " + mAnimationType.toString());

        // Run the Animation
        step();

    }

最终调用confirmSearchType(),它打开Enum以选择适当的算法:

public void determineSearchType(Pixel p) {
    // Animate a single pixel movement, step depends on AnimationType
    if (DEBUG && DEBUG_STEP_NEXT_PIXEL) { System.out.println("Determining Animation Type..."); }
    switch (mAnimationType) {

        case BREADTH_FIRST_ANIMATION:
            if (DEBUG && DEBUG_STEP_NEXT_PIXEL) { System.out.println("Animation Type: Breadth-First"); }
            // Begin Breadth-First Search
            stepBreadthSearch(p);
            break;

        case DEPTH_FIRST_ANIMATION:
            if (DEBUG && DEBUG_STEP_NEXT_PIXEL) { System.out.println("Animation Type: Depth-First"); }
            // Begin Depth-First Search
            stepDepthSearch(p);
            //simpleDepthSearch(mCurrentPixel);
            break;  
    }
}

当我交替评论它们时,每个线程/动画都在自己的JPanel / Canvas图形中执行并产生预期的结果。我对线程很新,我确信有人有一个简单的解决方案。关于如何解决动画同时激活的问题的任何想法?

1 个答案:

答案 0 :(得分:-1)

选项1:

给予另一个Thread执行机会。在线程代码的最后,尝试yield(),看看你是否有运气

Thread.currentThread().yield();

选项2:

在你的线程中添加标志,暂停并继续线程。这个想法是

线程1完成步骤后

  - 暂停线程1 - 然后启动线程2 - 在线程2完成步骤之后   - 暂停线程2 - 再次继续线程1。