重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用e.printStackTrace();
成都创新互联主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、全网整合营销推广、网站程序开发、HTML5响应式成都网站建设、移动网站建设、微商城、网站托管及成都网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为发电机回收行业客户提供了网站推广服务。
如果你是说你程序中的用到的堆栈,可以在finally{}块中把堆栈的内容输出来。
StackTrace ss = new StackTrace(true);
String flName = ss.GetFrame(1).GetFileName();// GetMethod().DeclaringType;
int lineNo = ss.GetFrame(1).GetFileLineNumber();
String methodName = ss.GetFrame(1).GetMethod().Name;
这是 C# 的调用栈上的内容
java 中也有一个,
StackTraceElement calledSte = getCallStackTraceElement();
int lineNo = calledSte.getLineNumber();
String className = calledSte.getClassName();
String methodName = calledSte.getMethodName();
Stack.java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Stack {
private int[] data;
private int index;
private Lock lock;
private Condition moreSpace;
private Condition moreEelment;
public Stack(int size){
this.data = new int[size];
this.index = 0;
this.lock = new ReentrantLock();
this.moreSpace = lock.newCondition();
this.moreEelment = lock.newCondition();
}
public void push(int value){
lock.lock();
try {
while(index == data.length){
moreSpace.await();
}
data[index++] = value;
moreEelment.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public int popup(){
lock.lock();
int value = 0;
try {
while(index == 0){
moreEelment.await();
}
value = data[--index];
moreSpace.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return value;
}
}
写入线程 WriteStack.java
import java.util.Random;
public class WriteStack implements Runnable {
private Stack stack;
public WriteStack(Stack stack){
this.stack = stack;
}
@Override
public void run() {
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i 10; i++){
int value = r.nextInt(500);
stack.push(value);
System.out.printf("Write: push %d in stack\n",value);
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
读取线程 ReadStack.java
import java.util.Random;
public class ReadStack implements Runnable {
private Stack stack;
public ReadStack(Stack stack){
this.stack = stack;
}
@Override
public void run() {
Random r = new Random(System.currentTimeMillis());
for(int i = 0;i 10; i++){
int value = stack.popup();
System.out.printf("Read: popup an element %d\n",value);
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主测试线程 StackExample.java
public class StackExample {
public static void main(String[] args) throws InterruptedException {
Stack stack = new Stack(5);
WriteStack writeStack = new WriteStack(stack);
ReadStack readStack = new ReadStack(stack);
Thread writeThread = new Thread(writeStack);
Thread readThread = new Thread(readStack);
writeThread.start();
readThread.start();
}
}
可以利用LinkedList 来写一个栈. 例如
import java.util.* ;
public class testStack{
private LinkedList list = new LinkedList() ;
public void push(Object o ){
try{
list.addFirst( o ) ;
}catch(Exception e){
}
}
public Object pop(){
return list.removeFirst() ;
}
public Object top() {
return list.getLast() ;
}
public static void main(String[] args){
//code here
testStack s = new testStack() ;
for( int i = 0 ; i10 ;i++){
s.push( i );
}
System.out.println ( s.pop());
System.out.println(s.top());
}
}
你可以好好看看Thinking in JAVA中对集合的讲解。。