62,621
社区成员
发帖
与我相关
我的任务
分享
while (true) {
synchronized (this) { // 使用同步
if (index >= 10)
break;
System.out.println("线程" + Thread.currentThread().getName()
+ "---------" + "ABCABC.....");
index++;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class apple1 implements Runnable
{
int index = 0; //使用共同资源
public void run()
{
//for(int a=0;a<10;a++)
while(true)
{
synchronized(this) { //使用同步
if (index >= 10) break;
System.out.println("线程"+Thread.currentThread().getName()+"---------"+"ABCABC.....");
index++;
}
}
}
};