一、前言
通过jasperreports源码中net.sf.jasperreports.engine.util.ThreadLocalStack实现线程栈源码分析,基于java.util.LinkedList自定义实现堆栈和队列,详情参见源码说明部分。
二、源码说明
1.ThreadLocalStack源码类
package net.sf.jasperreports.engine.util;@b@@b@import java.util.LinkedList;@b@@b@public class ThreadLocalStack@b@{@b@ private final ThreadLocal threadStack;@b@@b@ public ThreadLocalStack()@b@ {@b@ this.threadStack = new ThreadLocal();@b@ }@b@@b@ public void push(Object o)@b@ {@b@ LinkedList stack = (LinkedList)this.threadStack.get();@b@ if (stack == null)@b@ {@b@ stack = new LinkedList();@b@ this.threadStack.set(stack);@b@ }@b@ stack.addFirst(o);@b@ }@b@@b@ public Object top()@b@ {@b@ Object o = null;@b@ LinkedList stack = (LinkedList)this.threadStack.get();@b@ if ((stack != null) && (!(stack.isEmpty())))@b@ {@b@ o = stack.getFirst();@b@ }@b@ return o;@b@ }@b@@b@ public Object pop()@b@ {@b@ Object o = null;@b@ LinkedList stack = (LinkedList)this.threadStack.get();@b@ if (stack != null)@b@ {@b@ o = stack.removeFirst();@b@ }@b@ return o;@b@ }@b@@b@ public boolean empty()@b@ {@b@ LinkedList stack = (LinkedList)this.threadStack.get();@b@ return ((stack == null) || (stack.isEmpty()));@b@ }@b@}
2.自定义堆栈MyStack类
import java.util.*;@b@@b@public class MyStack {@b@ @b@ private LinkedList list;@b@ @b@ public MyStack(){@b@ list=new LinkedList();@b@ }@b@ @b@ public Object top(){ //输出最上面的元素@b@ if(list.size()!=0){@b@ return list.getFirst();@b@ }@b@ return -1;@b@ }@b@ @b@ public void pop(){ //出栈@b@ if(list.size()!=0){@b@ list.removeFirst();@b@ }@b@ }@b@ @b@ public void push(Object v){ //入栈@b@ list.addFirst(v);@b@ }@b@ @b@ public int getLen(){@b@ return list.size();@b@ }@b@ @b@ public static void main(String[] args) {@b@ @b@ MyStack stack = new MyStack();@b@ stack.push("张三");@b@ stack.push(3);@b@ stack.push("李四");@b@ stack.push("5");@b@ System.out.println("长度---"+stack.getLen()); //4@b@ @b@ @b@ //正确,但是会引来不安全操作提醒@b@ while(stack.getLen()>0){ @b@ System.out.println(stack.top());@b@ stack.pop();@b@ }@b@ System.out.println(stack.top());@b@ stack.pop();@b@@b@ }@b@@b@}
控制台输出
长度---4@b@5@b@李四@b@3@b@张三@b@-1
3. 自定义队列MyQueue实现类
import java.util.LinkedList;@b@@b@public class MyQueue {@b@@b@ private LinkedList list = new LinkedList();@b@@b@ public void clear() {@b@ // 销毁队列@b@ list.clear();@b@ }@b@@b@ public boolean isEmpty() {@b@ // 判断队列是否为空@b@ return list.isEmpty();@b@ }@b@@b@ public void add(Object o) {@b@ // 进队@b@ list.addLast(o);@b@ }@b@@b@ public Object poll() {@b@ // 出队@b@ if (!list.isEmpty()) {@b@ return list.removeFirst();@b@ }@b@ return "队列为空";@b@ }@b@@b@ public int size() {@b@ // 获取队列长度@b@ return list.size();@b@ }@b@@b@ public Object peek() {@b@ // 查看队首元素@b@ return list.getFirst();@b@ }@b@@b@ public static void main(String[] args){@b@ @b@ // 测试队列@b@ @b@ MyQueue queue = new MyQueue();@b@ queue.add("a");@b@ queue.add("b");@b@ queue.add("c");@b@ queue.add("d");@b@ queue.add("e");@b@ queue.add("f");@b@ System.out.println(queue.size());@b@ System.out.println(queue.poll());@b@ System.out.println(queue.poll());@b@ System.out.println(queue.peek());@b@ System.out.println(queue.poll());@b@ queue.clear();@b@ queue.add("s");@b@ queue.add("t");@b@ queue.add("r");@b@ System.out.println(queue.poll());@b@ System.out.println(queue.size());@b@ System.out.println(queue.peek());@b@ System.out.println(queue.poll());@b@ }@b@}
控制台输出结果
6@b@a@b@b@b@c@b@c@b@s@b@2@b@t@b@t