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.

ThreadCounterImpl11.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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.List;
  16. import java.util.ArrayList;
  17. import java.util.Enumeration;
  18. import java.util.Hashtable;
  19. public class ThreadCounterImpl11 implements ThreadCounter {
  20. private Hashtable<Thread, Counter> counters = new Hashtable<>();
  21. private Thread cached_thread;
  22. private Counter cached_counter;
  23. private int change_count = 0;
  24. private static final int COLLECT_AT = 20000;
  25. private static final int MIN_COLLECT_AT = 100;
  26. static class Counter {
  27. protected int value = 0;
  28. }
  29. private synchronized Counter getThreadCounter() {
  30. if (Thread.currentThread() != cached_thread) {
  31. cached_thread = Thread.currentThread();
  32. cached_counter = counters.get(cached_thread);
  33. if (cached_counter == null) {
  34. cached_counter = new Counter();
  35. counters.put(cached_thread, cached_counter);
  36. }
  37. change_count++;
  38. // Collect more often if there are many threads, but not *too* often
  39. int size = Math.max(1, counters.size()); // should be >1 b/c always live threads, but...
  40. if (change_count > Math.max(MIN_COLLECT_AT, COLLECT_AT/size)) {
  41. List<Thread> dead_stacks = new ArrayList<>();
  42. for (Enumeration<Thread> e = counters.keys(); e.hasMoreElements(); ) {
  43. Thread t = e.nextElement();
  44. if (!t.isAlive()) dead_stacks.add(t);
  45. }
  46. for (Thread t : dead_stacks) {
  47. counters.remove(t);
  48. }
  49. change_count = 0;
  50. }
  51. }
  52. return cached_counter;
  53. }
  54. public void inc() {
  55. getThreadCounter().value++;
  56. }
  57. public void dec() {
  58. getThreadCounter().value--;
  59. }
  60. public boolean isNotZero() {
  61. return getThreadCounter().value!=0;
  62. }
  63. public void removeThreadCounter() {
  64. // TODO
  65. }
  66. }