Austin Hsieh
Austin Hsieh

Reputation: 15

Java UI thread freezing despite code being in separate thread

I'm running some webcam captures in a thread like so:

class Capture implements Runnable { 
    @Override
    public void run() {
        while(true){
            //capture images
            //sleep 5 seconds
    }
}
//To actually start the capture
new Capture().run();

I'm doing this constantly, so I expect to still be able to perform UI functions like clicking on buttons while this is going on, but that's not the case. The x button on my JFrame is unresponsive, and same with other UI components.

Do I need to do something other than just using a separate thread? Doesn't seem to be working for me. Thanks

Upvotes: 0

Views: 67

Answers (2)

user2575725
user2575725

Reputation:

You have just implemented Runnable. You haven't started a Thread to do the job. Try this:

new Thread(new Capture()).start();

Also consider Timer class for such job.

Upvotes: 2

This is happening because you are calling the run method but not starting the Thread when you do new Capture().run(); you are not even creating an instance of the Thread

Do I need to do something other than just using a separate thread?

yes, create and start the thread doing instead

new Thread(new Capture()).start();

Upvotes: 1

Related Questions