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ů.

ThreadStackImpl11.java 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.Enumeration;
  16. import java.util.Hashtable;
  17. import java.util.Stack;
  18. public class ThreadStackImpl11 implements ThreadStack {
  19. private Hashtable stacks = new Hashtable();
  20. private Thread cached_thread;
  21. private Stack cached_stack;
  22. private int change_count = 0;
  23. private static final int COLLECT_AT = 20000;
  24. private static final int MIN_COLLECT_AT = 100;
  25. public synchronized Stack getThreadStack() {
  26. if (Thread.currentThread() != cached_thread) {
  27. cached_thread = Thread.currentThread();
  28. cached_stack = (Stack)stacks.get(cached_thread);
  29. if (cached_stack == null) {
  30. cached_stack = new Stack();
  31. stacks.put(cached_thread, cached_stack);
  32. }
  33. change_count++;
  34. // Collect more often if there are many threads, but not *too* often
  35. int size = Math.max(1, stacks.size()); // should be >1 b/c always live threads, but...
  36. if (change_count > Math.max(MIN_COLLECT_AT, COLLECT_AT/size)) {
  37. Stack dead_stacks = new Stack();
  38. for (Enumeration e = stacks.keys(); e.hasMoreElements(); ) {
  39. Thread t = (Thread)e.nextElement();
  40. if (!t.isAlive()) dead_stacks.push(t);
  41. }
  42. for (Enumeration e = dead_stacks.elements(); e.hasMoreElements(); ) {
  43. Thread t = (Thread)e.nextElement();
  44. stacks.remove(t);
  45. }
  46. change_count = 0;
  47. }
  48. }
  49. return cached_stack;
  50. }
  51. public void removeThreadStack() {
  52. // TODO
  53. }
  54. }