fork download
  1. import java.time.*;
  2. import java.util.concurrent.locks.*;
  3.  
  4. class Test
  5. {
  6. static Lock lock = new ReentrantLock();
  7.  
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. Thread worker = new Thread(() -> {
  11. try {
  12. for (int i = 0; i < 20; i++) {
  13. Thread.sleep(20);
  14. lock.lock();
  15. lock.unlock();
  16. System.out.println(LocalTime.now());
  17. }
  18. }
  19. catch (InterruptedException ex) { }
  20. });
  21. worker.start();
  22. Thread.sleep(100);
  23. System.out.println("Locking...");
  24. lock.lock();
  25. Thread.sleep(500);
  26. lock.unlock();
  27. System.out.println("Unlocked");
  28. worker.join();
  29. }
  30. }
Success #stdin #stdout 0.18s 320832KB
stdin
Standard input is empty
stdout
15:36:57.511
15:36:57.538
15:36:57.559
15:36:57.580
Locking...
Unlocked
15:36:58.089
15:36:58.109
15:36:58.130
15:36:58.151
15:36:58.172
15:36:58.193
15:36:58.214
15:36:58.235
15:36:58.256
15:36:58.277
15:36:58.298
15:36:58.319
15:36:58.340
15:36:58.361
15:36:58.382
15:36:58.403