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.