源程序如下所示
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

解决方案 »

  1.   

    线程队列里的排序是第一种情况,你在第一个线程start()之后,程序sleep()10秒后,再启动第二个线程,结果就对了!
      

  2.   

    你的CPU是不是双核的,如果是双核的很正常,会有两个线程同时在CPU里面运行
      

  3.   

    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(); } } 
    我都是菜鸟,互相帮助下!那个run方法是什么时候被使用的啊!我没有看到在main里面有调用他的啊! 你的问题我在6楼回答你了!
      

  4.   

    run()是再执行start()后执行的,有JVM调用。
      

  5.   

    那个 我找到答案了!呵呵!我总是一边问问题一边自己找答案!
             线程开始执行;Java 虚拟机调用该线程的 run 方法。(创建线程的时候自动调用) 也许是这样的哈!
      

  6.   

    把书拿出来好好看看线程那部分,摘抄部分给你:
    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中,至于它啥时候高兴叫谁运行,是不可预知也是每次不相同的。如果你要控制,就需要深入研究线程的几个状态。
      

  7.   

    结果没有问题,至于先后顺序不是你能控制的,这要看CPU的心情了。
      

  8.   

    code=Java]
        public synchronized void start() {
            if (started)
                throw new IllegalThreadStateException();
            started = true;
            group.add(this);
            start0();
        }    private native void start0();[
    [/code]
      

  9.   


        public synchronized void start() {
            if (started)
                throw new IllegalThreadStateException();
            started = true;
            group.add(this);
            start0();
        }    private native void start0();[