4

I have attempted to pause the execution of part of my program for a few seconds by invoking the sleep() method of the Thread class. However, when I try this, the code that comes immediately after this invocation still executes immediately. For instance, if I have the code:

Thread compoundThread = new Thread(new Runnable()
{                       
    public void run() 
    {
        try 
        {
            Thread.currentThread().sleep(2000);
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            Thread.currentThread().interrupt();
        }               
    }

});

compoundThread.start();

System.out.println("line 15")

Line 15 just prints immediately. Is there a way around this problem? I thought the whole idea of the sleep() method was to pause execution.

Edit in response to Philipp's comment

while (totalNoOfPhase1Trials > 399)
{           //
                Phase1Trial phase1Trial = new Phase1Trial(numberOfElements, elementColors);

                displayComplexStimulus(phase1Trial.getComplexStimulus());
                validate();

                Thread compoundThread = new Thread(new Runnable(){

                    public void run() {
                        try {
                            Thread.currentThread().sleep(2000);
                            System.out.println("line 226");
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block

                        }


                    }

                });



                compoundThread.start();     
                try {
                    compoundThread.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

The displayCompoundStimulus method displays a set of jlabels with no delay when I don't include the code you just mentioned where I put the thread to sleep. However, when I include the code that puts the thread to sleep, the display of images gets delayed by two seconds as well, which is strange given that Thread.sleep() gets executed AFTER the images are displayed.

Aakash Goplani
  • 335
  • 1
  • 7
  • 17
lb91
  • 109
  • 9

6 Answers6

3

The code behaves as implemented. You are running your main thread, which starts another thread, which sleeps for 2000ms.

So your main thread does:

  1. start the compoundThread
  2. print out
  3. finish main thread

Depending on what you want to achieve, you could just remove the whole thread and do:

try {
   Thread.sleep(2000);
} catch (InterruptedException e) {
   // should not happen, you don't interrupt                     
}
System.out.println("line 15");

Otherwise you can move the output into the thread's run method:

Thread compoundThread = new Thread(new Runnable(){

    public void run() {
        try {
            Thread.sleep(2000);
            System.out.println("line 15");
        } catch (InterruptedException e) {
            // do whatever has to be done
        }
    }
});

compoundThread.start();

// you might want to wait until compoundThread is done
compoundThread.join();

UPDATE: Based on your comment regarding the delayed output

If you use the following code:

public static void main(String[] args) {
    System.out.println("Hello");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // should not happen, you don't interrupt
    }
    System.out.println("World");
}

The output will be "Hello" and two seconds after that "World". The thread will sleep for exactly two seconds.

Philipp
  • 1,129
  • 1
  • 13
  • 32
  • @siegi damn it I missed that solution :) – Philipp Mar 21 '16 at 05:53
  • Your suggestion about removing the whole thread creates problems for me, because it delays the execution of print statements that come before the Thread.sleep() line by two seconds as well, and I have no idea why. – lb91 Mar 21 '16 at 06:04
  • Could you provide your whole code to understand what you mean? – Philipp Mar 21 '16 at 06:09
  • Sorry, not a print statement, I mean images that I have displayed. My code displays a few jlabels in the jpanel. However, when I put the main thread to sleep through using Thread.sleep(), now, not just the print statement, but the image display gets delayed by two seconds as well, despite the fact that the images are supposed to be displayed before the thread is put to sleep, which makes no sense. The images display with no delay,but when I add the line of code: Thread.sleep() AFTER the line that displays the images, the image display also gets delayed by two seconds. – lb91 Mar 21 '16 at 06:11
  • 1
    @lb91: I guess you are using a `print` instead of a `println` before the `Thread.sleep()`. Java (to be precise the `PrintWriter`) does a flush automatically only when you use `println`. Use `System.out.flush()` to force a flush before you do `Thread.sleep()`. See also [this question](http://stackoverflow.com/q/7166328/1347968). – siegi Mar 21 '16 at 06:12
  • Please provide a full code example, otherwise it is hard to help you :(. – Philipp Mar 21 '16 at 06:14
  • can i display code in this comment section? Sorry, I am new to this. – lb91 Mar 21 '16 at 06:17
  • I assume that you are using a Swing UI? So have a look at http://stackoverflow.com/questions/8073823/forcing-java-to-refresh-the-java-swing-gui. The problem is probably that your main thread is blocked, because of the join. Thus, the Swing UI (or whatever UI framework your are using - does not redraw). Read through the stackoverflow answer (see link in this comment) and see if that helps! – Philipp Mar 21 '16 at 06:27
  • I'm confused. The main thread has already executed the code to display the images before it has a chance to be blocked by the join() method, does it not? – lb91 Mar 21 '16 at 06:32
  • No, because Swing UI code renders/refreshes the UI not directly when it is manipulated. Maybe these links help you to understand: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html, http://stackoverflow.com/questions/7229284/refreshing-gui-by-another-thread-in-java-swing, http://www.javamex.com/tutorials/threads/invokelater.shtml. The topic is going off topic, you should start a new question regarding multi threading and Swing (or whatever framework you are using). – Philipp Mar 21 '16 at 06:35
1

The main thread is the one that starts running your main method.

Your main method then creates a new Thread object and invokes compoundThread.start();, which causes the second thread to begin execution.

Immediately afterwards, without stopping the main thread continues execution and arrives at the print statement. Thus line 15 is being printed by the main thread.

The secnond thread you started sleeps for 2 seconds and then terminates. If you had put the print statement in the thread's run method after the sleep call, then there would have been a 2 second pause before printing line 15.

Jim Garrison
  • 81,234
  • 19
  • 144
  • 183
1

Here compoundThread is a new thread object. Your line 15 is executed through main method. So when you start compoundThread.start(), a new thread is created and started independently of your main thread.

If you want to slow down your main thread, just use Thread.sleep(2000) on outside the runnable object code.

Ram
  • 3,932
  • 2
  • 28
  • 37
  • I find that when I do that, it ends up slowing down the execution of code before the Thread.sleep(2000) by two seconds as well. For instance, if I put a print statement before the Thread.sleep(2000) method and a print statement after, both print statements will print at the same time, which makes no sense to me. – lb91 Mar 21 '16 at 05:55
  • @lb91 That's the idea. It depends upon when and which Thread you want to `sleep`. As other answers pointed out, If you use it outside, it will slow down your main thread, inside the runnable then it will slow down that particular instance of the thread. – Ram Mar 21 '16 at 06:07
0

Your original thread (main thread) starts a new thread, and the new thread sleeps for two seconds. The original thread doesn’t, which is why it prints "Line 15" immediately. So Sandeep’s suggestion, though terse, should work if you understand and apply it correctly. One other obvious possibility is to put the System.out.println statement inside the run method of the compoundThread.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
0

Screenshot

have a view & tell me wheather it worked or not

Sandeep
  • 388
  • 3
  • 23
-2

I suggest you to use simple

thread.sleep(2000)
Sandeep
  • 388
  • 3
  • 23
  • 1
    @Sandeep: Welcome to StackOverflow! You can always edit your answer (using the link below). Especially for posting code the comments do not fit well. Use a code block directly in the answer. See the [Markdown help](http://stackoverflow.com/editing-help) to find out how to format properly. – siegi Mar 21 '16 at 06:03