so I'm making the "Simon game" and I need a java code to pause the program for 0.5 sec to show to the user the buttons he needs to press on.
greenButton.setBackground(Color.GREEN);
//need to stop here
press = true;
You could use Thread.sleep(500) to wait for 0.5 seconds.....and in another thread display the buttons to the user.....Or you can set a volatile boolean flag which gets activated when you show the user the button he needs to click on....and which pauses all other thread....once the user clicks on the button the flag should be unset and all other threads should be notified.
Since this looks to be Swing, use a Swing Timer to pause without freezing the program.
int delayTime = 500; // for 500 msecs
new Timer(delayTime, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: code to be delayed goes here
// stop the timer from repeating
((Timer) e.getSource()).stop();
}
}).start();
You can use the CountDownLatch API https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
For eg. In the first thread, create a latch with counter as 1 and pass it to the second thread that handles UI. Then in the first thread, call await() on the latch. This will cause the first thread to wait for the count to become zero. In parallel, in the second thread, you can handle he UI event and there you can do latch.countDown(). Once the count goes to zero, thread 1 will become active again. You can also provide a timeout in thread one. Thread One will come out of the wait and resume processing if the time out happens.
For more info, see this