/** * Clears the stack for this thread. */ public static void clearStack() { contextStack.clear(); }
/** * Removes the diagnostic context for this thread. * * <p> * Each thread that created a diagnostic context by calling {@link #push} should call this method before exiting. * Otherwise, the memory used by the <b>thread</b> cannot be reclaimed by the VM. * </p> * * <p> * As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that * the remove method is called before exiting a thread, this method has been augmented to lazily remove references * to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call * {@link #remove} before exiting a thread. However, you must call <code>remove</code> sometime. If you never call * it, then your application is sure to run out of memory. * </p> */ public static void removeStack() { contextStack.clear(); }
/** * Sets this thread's stack. * * @param stack The stack to use. */ public static void setStack(final Collection<String> stack) { if (stack.isEmpty() || !useStack) { return; } contextStack.clear(); contextStack.addAll(stack); }
@Test public void testCopy() { final DefaultThreadContextStack stack = createStack(); final ThreadContextStack copy = stack.copy(); assertEquals(3, copy.size()); assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // clearing stack does not affect copy stack.clear(); assertTrue(stack.isEmpty()); assertEquals(3, copy.size()); // not affected assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // adding to copy does not affect stack copy.add("other"); assertEquals(4, copy.size()); // not affected assertTrue(stack.isEmpty()); // adding to stack does not affect copy stack.push("newStackMsg"); assertEquals(1, stack.size()); assertEquals(4, copy.size()); // not affected // clearing copy does not affect stack copy.clear(); assertTrue(copy.isEmpty()); assertEquals(1, stack.size()); }
@Test public void testCopy() { final MutableThreadContextStack stack = createStack(); final ThreadContextStack copy = stack.copy(); assertEquals(3, copy.size()); assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // clearing stack does not affect copy stack.clear(); assertTrue(stack.isEmpty()); assertEquals(3, copy.size()); // not affected assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // adding to copy does not affect stack copy.add("other"); assertEquals(4, copy.size()); // not affected assertTrue(stack.isEmpty()); // adding to stack does not affect copy stack.push("newStackMsg"); assertEquals(1, stack.size()); assertEquals(4, copy.size()); // not affected // clearing copy does not affect stack copy.clear(); assertTrue(copy.isEmpty()); assertEquals(1, stack.size()); }
/** * Clears the stack for this thread. */ public static void clearStack() { contextStack.clear(); }
/** * Removes the diagnostic context for this thread. * * <p> * Each thread that created a diagnostic context by calling {@link #push} should call this method before exiting. * Otherwise, the memory used by the <b>thread</b> cannot be reclaimed by the VM. * </p> * * <p> * As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that * the remove method is called before exiting a thread, this method has been augmented to lazily remove references * to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call * {@link #remove} before exiting a thread. However, you must call <code>remove</code> sometime. If you never call * it, then your application is sure to run out of memory. * </p> */ public static void removeStack() { contextStack.clear(); }
/** * Sets this thread's stack. * * @param stack The stack to use. */ public static void setStack(final Collection<String> stack) { if (stack.isEmpty() || !useStack) { return; } contextStack.clear(); contextStack.addAll(stack); }