Java线程挂起 [英] Java Thread Hang

查看:79
本文介绍了Java线程挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在网上找到的一些2DTransitions创建Java幻灯片.一切运行正常,但是遇到一个问题,为什么在呈现幻灯片后尝试执行代码.我以为Java线程用于隔离进程,例如,我有一个类似的设置,并带有一个无限的while循环,而一个线程解决了该问题.在这里它不起作用,我不知道为什么.在下面的代码中,我转到了第一个输出,但是它挂在thread.run上,从不打印最后一个输出.

I'm creating a Java Slideshow using some 2DTransitions I found on the net. Everything work smoothly, but I come across a problem why trying to execute code after the Slideshow has been rendered. I thought Java threads are used to isolate processes, for instance I have a similar setup with an infinite while loop, and a thread solved that problem. Here it isn't working and I cant figure out why. In the code below I get to the first outputs, but it hangs on thread.run and never prints the last output.

有什么想法吗?

以下是相关代码:

System.out.println("Slideshow init");
        Thread thread = new Thread(new Runnable() {
  //TODO SLIDESHOW PROBLEM RIGHT HERE
            public void run(){
                int i=0;
                while(keepgoing){
                    if(i==images.size()-1){
                        transit(images.get(images.size()-1),images.get(0));
                        i=0;
                    }
                    else
                        transit(images.get(i),images.get(i+1));
                    i++;
                }       
            }
        });
        System.out.println("Thread created");
        thread.run();
        System.out.println("Thread started");`

推荐答案

它不是异步的.您需要调用start()而不是run().Run将在当前线程中执行Runnable的run方法. start将创建一个新线程,该线程将调用run方法.

It's not asynchronous. You need to invoke start() not run() Run will execute the Runnable's run method in the current thread. start will create a new thread which will invoke the run method.

这篇关于Java线程挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆