同一个包下有如下2个类:
为什么程序的输出没有
m0m1m2m3m4m5m6m7m8m9
m10m11m12m13m14m15m16m17m18m19
m0m1m2m3m4m5m6m7m8m9
m10m11m12m13m14m15m16m17m18m19
而只是输出:
主线程的优先级是3
线程t1的优先级是5
线程t2的优先级是5public class Machine implements Runnable{
static StringBuffer log = new StringBuffer();
public void run() {
// TODO Auto-generated method stub
int count =0;
for(int i=0;i<20;i++){
log.append("m"+i);
if(++count%10==0){log.append("\n");}
}
}
}
public class Test implements Runnable{
static StringBuffer log = new StringBuffer();
public void run() {
// TODO Auto-generated method stub
int count =0;
for(int i=0;i<20;i++){
log.append(Thread.currentThread().getName()+":m"+i);
if(++count%10==0){log.append("\n");}
}
}

public static void main(String[] args) throws InterruptedException {
Machine machine1 = new Machine();
Machine machine2 = new Machine();
Thread t1 = new Thread(machine1);
Thread t2 = new Thread(machine2);
t1.setName("t1");
t2.setName("t2");
Thread main = Thread.currentThread();
main.setPriority(3);
System.out.println("主线程的优先级是"+main.getPriority());
System.out.println("线程t1的优先级是"+t1.getPriority());
System.out.println("线程t2的优先级是"+t2.getPriority());
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
Thread.sleep(500);
System.out.println(log);
}
}

解决方案 »

  1.   

    因为你的 log   是 Test 中 
     static StringBuffer log = new StringBuffer();
    的log 所以为空啊。
      

  2.   

    你在 Machine 中
    int count =0;
            for(int i=0;i<20;i++){
                log.append("m"+i);
                System.out.println(("m"+i);   
             if(++count%10==0){log.append("\n");}
            }就会有输出。。
      

  3.   

    为什么StringBuffer为空?我append了啊?不会保存吗?