[英]Java Threads are not running on separate objects
我正在做一个学校项目,涉及生成两个单独的Swing Canvas对象,这些对象在我设计的矩阵/网格数据结构的克隆副本上为广度/深度优先搜索算法设置动画。
我创建了一些有助于将Matrix / Grid转换为图形的类,这些类组合在SearchAnimation类中,该类充当ViewController来管理动画。 在下图中,它们显示在右侧(不在黄色背景区域中)。 每个SearchAnimation对象都包含一个JLabel,Canvas和White Background。
以下是布局的屏幕截图:
JFrame包含应用程序控制器类(ICL.java)中SearchAnimation类的两个实例。 这些动画必须同时运行。 我为每个动画创建了单独的线程,并向其传递了单独的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();
}
最终调用的方法将打开一个Enum来选择适当的算法的defineSearchType():
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 :
给另一个线程执行的机会。 在线程代码的末尾,尝试yield()看看是否有运气
Thread.currentThread().yield();
选项2:
在您的线程中添加标志,以暂停并继续该线程。 这个想法是
在线程1完成步骤之后-暂停线程1-然后启动线程2-在线程2完成步骤之后-暂停线程2-再次继续线程1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.