Skip to content

Commit

Permalink
Merge pull request #66 from GravityMatrix/feature/deque替换stack
Browse files Browse the repository at this point in the history
使用Deque替换Stack
  • Loading branch information
mouzt authored Jun 1, 2022
2 parents 5315ffd + ed7da4b commit f391d61
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@

import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.*;

/**
* @author muzhantong
* create on 2021/2/9 2:22 下午
*/
public class LogRecordContext {

private static final InheritableThreadLocal<Stack<Map<String, Object>>> variableMapStack = new InheritableThreadLocal<>();
private static final InheritableThreadLocal<Deque<Map<String, Object>>> variableMapStack = new InheritableThreadLocal<>();

public static void putVariable(String name, Object value) {
if (variableMapStack.get() == null) {
Stack<Map<String, Object>> stack = new Stack<>();
Deque<Map<String, Object>> stack = new ArrayDeque<>();
variableMapStack.set(stack);
}
Stack<Map<String, Object>> mapStack = variableMapStack.get();
Deque<Map<String, Object>> mapStack = variableMapStack.get();
if (mapStack.size() == 0) {
variableMapStack.get().push(new HashMap<>());
variableMapStack.get().push(Maps.newHashMap());
}
variableMapStack.get().peek().put(name, value);
variableMapStack.get().element().put(name, value);
}

public static Object getVariable(String key) {
Map<String, Object> variableMap = variableMapStack.get().peek();
return variableMap.get(key);
return variableMap == null ? null : variableMap.get(key);
}

public static Map<String, Object> getVariables() {
Stack<Map<String, Object>> mapStack = variableMapStack.get();
Deque<Map<String, Object>> mapStack = variableMapStack.get();
return mapStack.peek();
}

Expand All @@ -47,9 +45,9 @@ public static void clear() {
* 每进入一个方法初始化一个 span 放入到 stack中,方法执行完后 pop 掉这个span
*/
public static void putEmptySpan() {
Stack<Map<String, Object>> mapStack = variableMapStack.get();
Deque<Map<String, Object>> mapStack = variableMapStack.get();
if (mapStack == null) {
Stack<Map<String, Object>> stack = new Stack<>();
Deque<Map<String, Object>> stack = new ArrayDeque<>();
variableMapStack.set(stack);
}
variableMapStack.get().push(Maps.newHashMap());
Expand Down

0 comments on commit f391d61

Please sign in to comment.