我创建了5个线程,每个线程循环输出一个字符串10次,大家帮我看看代码哪有错误,我写的代码输出是这样的:
bixiangdong
bixxxiixiangdong
bixiangdoaniiagndnagong
g
dnobbing
gdong
bixiangdong
xiangdong
bixiangdong
bixiangdong
bixiangdong
bixiangdong
bixixiiangdoanngdong
bixiangdong
bixiangdong
g
bbiixiaxniangdong
bixiangdong
bixgdoiangdong
bixiangdonng
g
bixiangdong
bixiangdong
bixiangdong
-------------------------------》
public class ThreadDemo {
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
for(int i=0;i<5;i++){
td.init();
}
}
private void init(){
final Output op = new Output();
new Thread(){
public void run(){
for(int i=0;i<10;i++)
op.output("bixiangdong");
}
}.start();
}
class Output{
public void output(String info){
synchronized(this){
for(int i=0;i<info.length();i++){
System.out.print(info.charAt(i));
}
System.out.println();
}}
}
} |
|