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.

CaseEConcurrent.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // TESTING: multiple instances causing factory invocation multiple times (but is cached correctly)
  2. // Concurrency fix regression test for https://github.com/eclipse/org.aspectj/issues/198
  3. import org.aspectj.lang.annotation.*;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. public class CaseEConcurrent {
  6. private String id;
  7. public static void main(String[]argv) throws InterruptedException {
  8. final CaseEConcurrent cea = new CaseEConcurrent("a");
  9. final CaseEConcurrent ceb = new CaseEConcurrent("b");
  10. Thread t1 = new Thread(new Runnable() { public void run() { ((I)cea).methodOne(); } });
  11. Thread t2 = new Thread(new Runnable() { public void run() { ((I)cea).methodTwo(); } });
  12. Thread t3 = new Thread(new Runnable() { public void run() { ((I)ceb).methodOne(); } });
  13. Thread t4 = new Thread(new Runnable() { public void run() { ((I)ceb).methodTwo(); } });
  14. t1.start();
  15. t2.start();
  16. t3.start();
  17. t4.start();
  18. t1.join();
  19. t2.join();
  20. t3.join();
  21. t4.join();
  22. }
  23. public CaseEConcurrent(String id) {
  24. this.id=id;
  25. }
  26. public String toString() {
  27. return "CaseEConcurrent instance: "+id;
  28. }
  29. // Helper methods 'doSomething' and 'callMe' help to produce byte code similar to what we need in order to fix
  30. // https://github.com/eclipse/org.aspectj/issues/198. If necessary, just temporarily uncomment, compile and analyse
  31. // the byte code, e.g. with JDK tool 'javap -v'.
  32. /*
  33. public void doSomething() {
  34. if (id == null) {
  35. synchronized (this) {
  36. if (id == null)
  37. id = "doing something";
  38. }
  39. }
  40. callMe(id);
  41. }
  42. public void callMe(String param) {
  43. System.out.println("I was called with param " + param);
  44. }
  45. */
  46. }
  47. aspect X {
  48. @DeclareMixin("CaseEConcurrent")
  49. public I createImplementation(Object o) {
  50. System.out.println("Delegate factory invoked for " + o);
  51. try { Thread.sleep(250); } catch (InterruptedException e) { throw new RuntimeException(e); }
  52. Implementation impl = new Implementation(o);
  53. return impl;
  54. }
  55. }
  56. interface I {
  57. void methodOne();
  58. void methodTwo();
  59. }
  60. class Implementation implements I {
  61. Object o;
  62. public Implementation(Object o) {
  63. this.o = o;
  64. }
  65. public void methodOne() {
  66. System.out.println("methodOne running on "+o);
  67. }
  68. public void methodTwo() {
  69. System.out.println("methodTwo running on "+o);
  70. }
  71. }