运行的结果是
child thread:thread[demo Thread,5,main]
main thread:5
child thread:5
child thread:4
main thread:4
child thread:3
child thread:2
main thead:3
child thread:1
exiting child thread.
main thread:2
main thread:1
main thread exiting
child thread:thread[demo Thread,5,main]
main thread:5
child thread:5
child thread:4
main thread:4
child thread:3
child thread:2
main thead:3
child thread:1
exiting child thread.
main thread:2
main thread:1
main thread exiting
解决方案 »
- 运行时Exception in thread "main" java.lang.NullPointerException
- Java提取网页内容中文为乱码的问题~~
- 终于忍不住问这个问题....
- 怎样把一个程序传到网上,通过外网能访问??
- 遇到个小问题 进来解答下
- 一堆的问题啊,无法解决! 类库跟API有什么区别啊?
- Frame鼠标点击异常
- 处学者的简单问题。错误:Exception in thread "main" java.lang.NosuchMethodError:main,带原码;
- 我给JButton加了一个KeyListener,为什么必须单击一次button后,listener才起作用?
- 线程同时启动讨论
- 出现下列异常是什么原因
- 能不能帮我找出错在那?
只不过newThread是由ThreadDemo控制
newThread 是子线程,当然是两个线程了
newThread 是子线程,当然是两个线程了你只看到一次 new Thread();
是的,那是你生成的Thread, 但是别忘了主线程(main())也是一个线程呀。可能你是初学者,我写了一个例子,可以运行的。package csdn;public class MyClass7 { public MyClass7() {
} public static void main(String[] args) {
MyClass7 myClass7 = new MyClass7();
myClass7.invokedStandalone = true; MyClass7_NewThread t0=new MyClass7_NewThread(" Thread First ");
MyClass7_NewThread t1=new MyClass7_NewThread(" Thread Second ");
}
private boolean invokedStandalone = false;
}
class MyClass7_NewThread implements Runnable
{
Thread t;
MyClass7_NewThread(String threadName){
t=new Thread(this,threadName);
System.out.println(threadName+" starting .... " );
t.start();
}
public void run()
{
try{
for(int i=5;i>0;i--){
System.out.println(t.getName()+" "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e){
System.out.println(t.getName ()+"interrupted.");
}
System.out.println("Exiting "+t.getName ());
}
}你会明白些。:》