fork download
  1. class SynchTest {
  2. public static void main(String[] args) {
  3. // Now we pass the same instance each time.
  4. Task t = new Task();
  5. new Thread(t).start();
  6. new Thread(t).start();
  7. new Thread(t).start();
  8. }
  9.  
  10. static class Task implements Runnable {
  11. long start;
  12.  
  13. Task() {
  14. this.start = System.currentTimeMillis();
  15. }
  16.  
  17. @Override
  18. public synchronized void run() {
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException ignored) {
  22. }
  23. System.out.println(System.currentTimeMillis() - start);
  24. }
  25. }
  26. }
Success #stdin #stdout 0.04s 2380800KB
stdin
Standard input is empty
stdout
1000
2001
3001