Austin Hsieh Austin Hsieh - 1 year ago 68
Java Question

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 {
public void run() {
//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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download