You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Basic.java 848B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Exploring synchronization
  2. public class Basic {
  3. public static void main(String[] args) {
  4. Basic b = new Basic();
  5. b.methodWithSyncBlock1();
  6. b.staticMethodWithSyncBlock1();
  7. b.methodWithSyncBlock2();
  8. b.staticMethodWithSyncBlock2();
  9. }
  10. public void methodWithSyncBlock1() {
  11. System.err.println("methodWithSyncBlock1");
  12. synchronized (this) {
  13. }
  14. }
  15. public void staticMethodWithSyncBlock1() {
  16. System.err.println("staticMethodWithSyncBlock1");
  17. synchronized (Basic.class) {
  18. }
  19. }
  20. public void methodWithSyncBlock2() {
  21. System.err.println("methodWithSyncBlock2");
  22. synchronized (this) {
  23. int i = 0;
  24. while (i<100) {
  25. i++;
  26. }
  27. }
  28. }
  29. public void staticMethodWithSyncBlock2() {
  30. System.err.println("staticMethodWithSyncBlock2");
  31. synchronized (Basic.class) {
  32. int i = 0;
  33. while (i<100) {
  34. i++;
  35. }
  36. }
  37. }
  38. }