class TestThreadMethod extends Thread{
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public synchronized void run(){
for(int i=0; i <=40; i++){ //每个线程sleep以后,重新进入,那里保存了上一次本线程的i值
//如果是栈,那么只能取栈顶元素。那么肯定不行吧??
System.out.print(Thread.currentThread().getName());
System.out.println(" : " + i);
if(i==40) System.out.print(Thread.currentThread().getName()
+" over\n**********************\n");
try{ Thread.sleep(20); }
catch(InterruptedException e) {}
}
}
}
class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
TestThreadMethod t3 = new TestThreadMethod("t3");
t1.start();
t2.start();
t3.start();
t1.setPriority(Thread.MAX_PRIORITY);
}
}
public static int shareVar = 0;
public TestThreadMethod(String name){
super(name);
}
public synchronized void run(){
for(int i=0; i <=40; i++){ //每个线程sleep以后,重新进入,那里保存了上一次本线程的i值
//如果是栈,那么只能取栈顶元素。那么肯定不行吧??
System.out.print(Thread.currentThread().getName());
System.out.println(" : " + i);
if(i==40) System.out.print(Thread.currentThread().getName()
+" over\n**********************\n");
try{ Thread.sleep(20); }
catch(InterruptedException e) {}
}
}
}
class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1");
TestThreadMethod t2 = new TestThreadMethod("t2");
TestThreadMethod t3 = new TestThreadMethod("t3");
t1.start();
t2.start();
t3.start();
t1.setPriority(Thread.MAX_PRIORITY);
}
}
解决方案 »
- Java Applet程序里可以放Flash文件吗?
- 新手上路,谁能帮我解释这段代码????
- 请问getbyte(colmnname)如何使用,我不知道数据库中的字段的类型,如何读取其值??
- 关于jsp页面刷新和点击地址栏右边的箭头的区别?
- 问一个简单的问题
- 一个简单的Java画图程序,为何不能正常显示?
- 高手来帮我一下,一个小程序
- 请高手指点
- java调用webservice问题 我在用客户端调用axis2发布的webservice时候老是出错
- 请问谁告诉我这是用什么做的,能不能把原代码给我?http://www.madeinwx.com.cn/gyninfo/babysails/wish/wish.htm
- java 菜鸟来问个问题!!!
- 关于字符串 菜鸟求助
每次操作只取出自己的i 然后++ 没有问题啊
Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread.3 A Java virtual machine stack stores frames (§3.6). A Java virtual machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous.
The Java virtual machine specification permits Java virtual machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java virtual machine stacks are of a fixed size, the size of each Java virtual machine stack may be chosen independently when that stack is created. A Java virtual machine implementation may provide the programmer or the user control over the initial size of Java virtual machine stacks, as well as, in the case of dynamically expanding or contracting Java virtual machine stacks, control over the maximum and minimum sizes.4 The following exceptional conditions are associated with Java virtual machine stacks:
If the computation in a thread requires a larger Java virtual machine stack than is permitted, the Java virtual machine throws a StackOverflowError. If Java virtual machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java virtual machine stack for a new thread, the Java virtual machine throws an OutOfMemoryError.