fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static final SyncNotsynced sn = new SyncNotsynced();
  11.  
  12. public static void main(String[] args){
  13. Thread t1 = new Thread(sn::synced);
  14. Thread t2 = new Thread(sn::notsynced);
  15. t1.start();
  16. t2.start();
  17. }
  18.  
  19. public static class SyncNotsynced {
  20.  
  21. public synchronized void synced(){
  22. System.out.println(Thread.currentThread().getName() + " enter synced");
  23. try {
  24. Thread.sleep(2000);
  25. } catch (InterruptedException e) {
  26. Thread.currentThread().interrupt();
  27. }
  28. System.out.println(Thread.currentThread().getName() + " exit synced");
  29. }
  30.  
  31. public void notsynced(){
  32. System.out.println(Thread.currentThread().getName() + " enter notsynced");
  33. try {
  34. Thread.sleep(2000);
  35. } catch (InterruptedException e) {
  36. Thread.currentThread().interrupt();
  37. }
  38. System.out.println(Thread.currentThread().getName() + " exit notsynced");
  39. }
  40. }
  41. }
Success #stdin #stdout 0.19s 33320KB
stdin
Standard input is empty
stdout
Thread-0 enter synced
Thread-1 enter notsynced
Thread-0 exit synced
Thread-1 exit notsynced