I wan't to be sure that some piece of my code within the main thread will be executed after some piece of code executed withing the secondary thread. Here's what I got:
final Object lock = new Object();
final Thread t = new Thread(new Runnable() {
public void run() {
synchronized(lock) {
System.out.println("qwerty");
lock.notify();
}
}
});
synchronized(lock) {
t.start();
lock.wait();
}
System.out.println("absolutely sure, qwerty is above");
- Is it correct solution?
- Any shorter ways to do the same?