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.

Basic2.java 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Exploring synchronization
  2. aspect WithinAspect {
  3. before(): within(Basic2) {
  4. if (thisJoinPoint.toString().indexOf("lock(")!=-1)
  5. System.err.println("Advice running at "+thisJoinPoint.getSignature());
  6. }
  7. }
  8. public class Basic2 {
  9. public static void main(String[] args) {
  10. Basic2 b = new Basic2();
  11. b.methodWithSyncBlock1();
  12. b.staticMethodWithSyncBlock1();
  13. b.methodWithSyncBlock2();
  14. b.staticMethodWithSyncBlock2();
  15. }
  16. public void methodWithSyncBlock1() {
  17. System.err.println("methodWithSyncBlock1");
  18. synchronized (this) {
  19. }
  20. }
  21. public void staticMethodWithSyncBlock1() {
  22. System.err.println("staticMethodWithSyncBlock1");
  23. synchronized (Basic2.class) {
  24. }
  25. }
  26. public void methodWithSyncBlock2() {
  27. System.err.println("methodWithSyncBlock2");
  28. synchronized (this) {
  29. int i = 0;
  30. while (i<100) {
  31. i++;
  32. }
  33. }
  34. }
  35. public void staticMethodWithSyncBlock2() {
  36. System.err.println("staticMethodWithSyncBlock2");
  37. synchronized (Basic2.class) {
  38. int i = 0;
  39. while (i<100) {
  40. i++;
  41. }
  42. }
  43. }
  44. }