黑马程序员技术交流社区
标题:
多个线程循环输出字符串问题求解
[打印本页]
作者:
周建
时间:
2012-3-18 15:24
标题:
多个线程循环输出字符串问题求解
我创建了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();
}}
}
}
作者:
张锐
时间:
2012-3-18 17:21
锁的问题,你new了五个对象,每个对象都是用自己的锁,没有实现同步的功能,把 synchronized(this) 改成synchronized(ThreadDemo.class) 这样锁就唯一了就可以实现你的需求。
作者:
贠(yun)靖
时间:
2012-3-18 17:38
public class ThreadDemo {
private boolean flag = false;
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(){
op.output("bixiangdong");
}
}.start();
}
class Output{
public void output(String info){
synchronized(this)
{
for (int j=0;j<info.length() ;j++ )
{
synchronized(this)
{
System.out.print(Thread.currentThread().getName()+"=");
for(int i=j;i<info.length();i++)
{
System.out.print(info.charAt(i));
}
}
System.out.println();
}
}
}
}
}
/*
---------- java ----------
Thread-0=bixiangdong
Thread-0=ixiangdong
Thread-0=xiangdong
Thread-0=iangdong
Thread-0=angdong
Thread-0=ngdong
Thread-0=gdong
Thread-0=dong
Thread-0=ong
Thread-0=ng
Thread-0=g
Thread-3=bixiangdong
Thread-3=ixiangdong
Thread-3=xiangdong
Thread-3=iangdong
Thread-3=angdong
Thread-3=ngdong
Thread-3=gdong
Thread-3=dong
Thread-3=ong
Thread-3=ng
Thread-3=g
Thread-1=bixiangdong
Thread-1=ixiangdong
Thread-1=xiangdong
Thread-1=iangdong
Thread-1=angdong
Thread-1=ngdong
Thread-1=gdong
Thread-1=dong
Thread-1=ong
Thread-1=ng
Thread-1=g
Thread-4=bixiangdong
Thread-4=ixiangdong
Thread-4=xiangdong
Thread-4=iangdong
Thread-4=angdong
Thread-4=ngdong
Thread-4=gdong
Thread-4=dong
Thread-4=ong
Thread-4=ng
Thread-4=g
Thread-2=bixiangdong
Thread-2=ixiangdong
Thread-2=xiangdong
Thread-2=iangdong
Thread-2=angdong
Thread-2=ngdong
Thread-2=gdong
Thread-2=dong
Thread-2=ong
Thread-2=ng
Thread-2=g
输出完毕 (耗时 0 秒) - 正常终止
不知道你需求是什么,感觉你应该是想一个线程一次输出递增或者递减吧 所以帮你改了下
如果要打乱5个线程在内循环的输出的话 可以不加外层循环的锁
结果是
---------- java ----------
Thread-0=bixiangdong
Thread-0=ixiangdongThread-2=bixiangdong
Thread-1=
bixiangdongThread-4=bixian
gdong
Thread-3=bixiangdong
Thread-3=ixiangdong
Thread-0=xiangdong
Thread-2=ixiangdongThread-0=iangdong
Thread-3=xiangdongThread-4=ixiangdong
Thread-4=xiangdong
Thread-1=ixiangdong
Thread-2=xiangdong
Thread-4=iangdongThread-0=angdo
ng
Thread-4=angdong
Thread-3=iangdong
Thread-2=iangdong
Thread-2=angdong
Thread-1=xiangdong
Thread-2=ngdong
Thread-3=angdong
Thread-4=ngdong
Thread-0=ngdong
Thread-4=gdong
Thread-3=ngdong
Thread-3=gdongThread-2=gdong
Thread-1=iangdong
Thread-2=dong
Thread-3=dong
Thread-3=ong
Thread-4=dongThread-0=gdong
Thread-0=dong
Thread-0=ong
Thread-0=ng
Thread-3=ngThread-2=ong
Thread-2=ng
Thread-2=g
Thread-1=angdong
Thread-3=gThread-4=ong
Thread-0=g
Thread-4=ng
Thread-4=g
Thread-1=ngdong
Thread-1=gdong
Thread-1=dong
Thread-1=ong
Thread-1=ng
Thread-1=g
输出完毕 (耗时 0 秒) - 正常终止
*/
作者:
段浩亮
时间:
2012-3-18 21:00
二楼正解,五个线程各有各的锁
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2