[求助]为什么程序不输出run()方法内容?

Steve_Samuelson 2008-04-22 08:53:12
同一个包下有如下2个类:
为什么程序的输出没有
m0m1m2m3m4m5m6m7m8m9
m10m11m12m13m14m15m16m17m18m19
m0m1m2m3m4m5m6m7m8m9
m10m11m12m13m14m15m16m17m18m19
而只是输出:
主线程的优先级是3
线程t1的优先级是5
线程t2的优先级是5

public 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);
}
}
...全文
63 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Steve_Samuelson 2008-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 caoyinghui1986 的回复:]
因为你的 log 是 Test 中
static StringBuffer log = new StringBuffer();
的log 所以为空啊。
[/Quote]
为什么StringBuffer为空?我append了啊?不会保存吗?
Shine_Panda 2008-04-22
  • 打赏
  • 举报
回复
你在 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");}
}

就会有输出。。
wangshikang_it 2008-04-22
  • 打赏
  • 举报
回复
学习
Shine_Panda 2008-04-22
  • 打赏
  • 举报
回复
因为你的 log 是 Test 中
static StringBuffer log = new StringBuffer();
的log 所以为空啊。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧