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.

Application.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.concurrent.atomic.AtomicInteger;
  5. /**
  6. * https://github.com/eclipse-aspectj/aspectj/issues/279
  7. */
  8. public class Application {
  9. public static AtomicInteger HELLO_COUNT = new AtomicInteger(0);
  10. public static AtomicInteger ASPECT_COUNT = new AtomicInteger(0);
  11. private static final int ROUNDS = 25;
  12. private static final int THREAD_COUNT = 2;
  13. private static final int TOTAL_COUNT = ROUNDS * THREAD_COUNT;
  14. private static final String CLASS_TO_LOAD = "GreeterImpl";
  15. public static void main(String[] args) throws Exception {
  16. for (int round = 0; round < ROUNDS; round++) {
  17. ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
  18. ParallelCapableClassLoader cl = new ParallelCapableClassLoader(Application.class.getClassLoader(), CLASS_TO_LOAD);
  19. for (int i = 0; i < THREAD_COUNT; i++) {
  20. executor.submit(() -> {
  21. try {
  22. Class<?> myClass = Class.forName(CLASS_TO_LOAD, true, cl);
  23. Greeter greeter = (Greeter) myClass.getConstructor(new Class<?>[] {}).newInstance();
  24. greeter.hello();
  25. HELLO_COUNT.incrementAndGet();
  26. }
  27. catch (Exception e) {
  28. throw new RuntimeException(e);
  29. }
  30. });
  31. }
  32. executor.shutdown();
  33. executor.awaitTermination(60, TimeUnit.SECONDS);
  34. }
  35. assert HELLO_COUNT.get() == TOTAL_COUNT
  36. : String.format("Hello count should be %s, but is %s", TOTAL_COUNT, HELLO_COUNT.get());
  37. assert ASPECT_COUNT.get() == TOTAL_COUNT
  38. : String.format("Aspect count should be %s, but is %s", TOTAL_COUNT, ASPECT_COUNT.get());
  39. }
  40. }