Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ThreadCounterImpl11.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. *
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Andy Clement initial implementation
  12. * Copied from bits of original CFlowStack
  13. * ******************************************************************/
  14. package org.aspectj.runtime.internal.cflowstack;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.ArrayList;
  18. import java.util.Enumeration;
  19. import java.util.Hashtable;
  20. public class ThreadCounterImpl11 implements ThreadCounter {
  21. private Hashtable counters = new Hashtable();
  22. private Thread cached_thread;
  23. private Counter cached_counter;
  24. private int change_count = 0;
  25. private static final int COLLECT_AT = 20000;
  26. private static final int MIN_COLLECT_AT = 100;
  27. static class Counter {
  28. protected int value = 0;
  29. }
  30. private synchronized Counter getThreadCounter() {
  31. if (Thread.currentThread() != cached_thread) {
  32. cached_thread = Thread.currentThread();
  33. cached_counter = (Counter)counters.get(cached_thread);
  34. if (cached_counter == null) {
  35. cached_counter = new Counter();
  36. counters.put(cached_thread, cached_counter);
  37. }
  38. change_count++;
  39. // Collect more often if there are many threads, but not *too* often
  40. int size = Math.max(1, counters.size()); // should be >1 b/c always live threads, but...
  41. if (change_count > Math.max(MIN_COLLECT_AT, COLLECT_AT/size)) {
  42. List dead_stacks = new ArrayList();
  43. for (Enumeration e = counters.keys(); e.hasMoreElements(); ) {
  44. Thread t = (Thread)e.nextElement();
  45. if (!t.isAlive()) dead_stacks.add(t);
  46. }
  47. for (Iterator e = dead_stacks.iterator(); e.hasNext(); ) {
  48. Thread t = (Thread)e.next();
  49. counters.remove(t);
  50. }
  51. change_count = 0;
  52. }
  53. }
  54. return cached_counter;
  55. }
  56. public void inc() {
  57. getThreadCounter().value++;
  58. }
  59. public void dec() {
  60. getThreadCounter().value--;
  61. }
  62. public boolean isNotZero() {
  63. return getThreadCounter().value!=0;
  64. }
  65. public void removeThreadCounter() {
  66. // TODO
  67. }
  68. }