Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

multithreading - Java Thread Hang

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.

Any ideas why?

Here is the relevant code:

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");`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To start a thread, call thread.start(). Calling thread.run() directly doesn't spawn a new thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...