import java.awt.*;

public class ToolkitTest{

public static void main(String... args) throws Exception{
if(args.length > 0)Toolkit.getDefaultToolkit();

Runtime.getRuntime().addShutdownHook(
new Thread(ToolkitTest::shutdown)
);
new Thread(ToolkitTest::task).start();
System.exit(0);
}

private static final Object lock = new Object();
private static int a = 0;

public static void shutdown(){
synchronized(lock){
if(a == 0){
a = 1;
}
}

//wait till the other thread allows me to exit.
while(true){
synchronized(lock){
if(a == 2){
break;
}
}
}
}

public static void task(){
//wait until the shutdown starts.
while(true){
synchronized(lock){
if(a == 1){
break;
}
}
}

try{
System.out.println(
Toolkit.getDefaultToolkit()
);
System.out.println("done. ");
}catch(Exception e){
System.out.println("exception: " + e);
}

//allow shutdown hook to exit.
while(true){
synchronized(lock){
a = 2;
break;
}
}
}
}