Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java create background thread which does something periodically

Is it possible to create a separate background thread which would separately do some stuff? I've tried the following program but it doesn't work as I expect.

public class Test {

    private static class UpdaterThread extends Thread {
        private final int TIMEOUT = 3000;

        public void run() {
            while (true) {
                try {
                    Thread.sleep(TIMEOUT);
                    System.out.println("3 seconds passed");
                } catch (InterruptedException ex) {
                }
            }
        }
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        try {
            Thread u = new UpdaterThread();
            u.start();
            while (true) {
                System.out.println("--");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

I expected that every 3 seconds "3 seconds passed" will be printed in the flow of multiple "--" strings. In fact "3 seconds passed" is never printed. Why? And how can I create a background thread which would do something independantly from the main thread?

like image 775
Sergey Avatar asked Jul 17 '12 10:07

Sergey


People also ask

How do I run a thread in the background?

thread. run() is going to execute the code in the thread's run method on the current thread. You want thread. start() to run thread in background.

What's the best way to execute code in the background in a separate thread?

You can use a Handler to enqueue an action to be performed on a different thread. To specify the thread on which to run the action, construct the Handler using a Looper for the thread. A Looper is an object that runs the message loop for an associated thread.

What is a background thread?

Background threads are identical to foreground threads with one exception: a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down.

When creating a new thread to do a task Why does the thread class use the Run method for the code to execute?

start() method causes this thread to begin execution, the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).


1 Answers

There are lot of answers but nobody says why his example was not working. System.out is output stream, so after you have started write to this stream JAVA locks it and all other threads will wait while lock is applied to stream. After the stream will have unlocked another thread will be able to work with this stream.

To make your example working you should add Thread.sleep into while loop in the main thread.

like image 90
Sergii Stotskyi Avatar answered Sep 18 '22 12:09

Sergii Stotskyi