Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Exploring synchronization
  2. aspect LockMonitor {
  3. long locktimer = 0;
  4. int iterations = 0;
  5. Object currentObject = null;
  6. long activeTimer;
  7. before(Useful2 o ): lock() && args(o) {
  8. activeTimer = System.currentTimeMillis();
  9. currentObject = o;
  10. }
  11. after(Useful2 o ): unlock() && args(o) {
  12. if (o!=currentObject) {
  13. throw new RuntimeException("Unlocking on incorrect thing?!?");
  14. }
  15. if (activeTimer!=0) {
  16. locktimer+=(System.currentTimeMillis()-activeTimer);
  17. iterations++;
  18. activeTimer=0;
  19. }
  20. }
  21. after() returning: execution(* main(..)) {
  22. System.err.println("Average time spent with lock over "+iterations+" iterations is "+
  23. (((double)locktimer)/
  24. ((double)iterations))+"ms");
  25. }
  26. }
  27. public class Useful2 {
  28. public static void main(String[] args) {
  29. Useful2 u = new Useful2();
  30. for (int i = 0; i < 20; i++) {
  31. u.methodWithSynchronizedBlock();
  32. }
  33. }
  34. public void methodWithSynchronizedBlock() {
  35. synchronized (this) {
  36. for (int ii=0;ii<1000000;ii++);
  37. }
  38. }
  39. }