源程序如下所示
class NumberThread extends Thread {
int num;
public NumberThread(int n){
num=n;
}
public void run() {
for(int k=0;k<10;k++)
System.out.println(num);
}}
public class Numbers{
public static void main(String args[]){
NumberThread number1,number2,number3,number4,number5;
number1 = new NumberThread(1); number1.start();//Thread类中包含start()方法。
number2 = new NumberThread(2); number2.start();
number3 = new NumberThread(3); number3.start();
number4 = new NumberThread(4); number4.start();
number5 = new NumberThread(5); number5.start();
}
}
我要问的问题是:为什么程序在Eclipse上运行结果是11111111113333333333555555555522222222224444444444而不是:
11111111112222222222333333333344444444445555555555
解决方案 »
- 使用jdk1.7,但是新语法却不能使用,这是为什么?
- java List.add()抛出空指针,帮帮我看看是怎么一回事?
- JAVA 控制台创建GUI程序如何设置窗体背景?
- 哪位解释一下Math.round/floor/round的用法
- 小弟想问怎么样用java drawLine把自己的学号和姓名输出到屏幕上?
- 画好图了,如何生成一个图片文件,比如bmp格式的
- BufferedReader.close()方法的作用
- 求汉字转化为拼音(全拼)代码
- 关于TreeSet集合框架无法添加自定义对象的问题.
- 求大神帮帮忙,关于读取txt文件,用string array
- Frame设置大小及定位的两种方法
- 如何自己的代碼中使用netbeans下那個顯示數據庫查詢結果的表格組件
int num;
public NumberThread(int n){
num=n;
} public void run() {
for(int k=0;k <10;k++)
System.out.println(num);
} }
public class Numbers{
public static void main(String args[]){
NumberThread number1,number2,number3,number4,number5;
number1 = new NumberThread(1); number1.start();//Thread类中包含start()方法。
number2 = new NumberThread(2); number2.start();
number3 = new NumberThread(3); number3.start();
number4 = new NumberThread(4); number4.start();
number5 = new NumberThread(5); number5.start(); } }
我都是菜鸟,互相帮助下!那个run方法是什么时候被使用的啊!我没有看到在main里面有调用他的啊! 你的问题我在6楼回答你了!
线程开始执行;Java 虚拟机调用该线程的 run 方法。(创建线程的时候自动调用) 也许是这样的哈!
Note that calling your thread's start() method doesn't immediately cause the thread to run; it just makes it eligibleto run.
就是说你调用线程的start并不是说立刻让他运行,你的start()只是注册了一个线程在thread scheduler中,至于它啥时候高兴叫谁运行,是不可预知也是每次不相同的。如果你要控制,就需要深入研究线程的几个状态。
public synchronized void start() {
if (started)
throw new IllegalThreadStateException();
started = true;
group.add(this);
start0();
} private native void start0();[
[/code]
public synchronized void start() {
if (started)
throw new IllegalThreadStateException();
started = true;
group.add(this);
start0();
} private native void start0();[