class apple1 implements Runnable
{
public void run()
{
for(int a=0;a<10;a++)
{
System.out.println("线程"+Thread.currentThread().getName()+"---------"+"ABCABC.....");
}
}
};
public class apple
{
public static void main(String[] args)
{
apple1 aa=new apple1();
Thread A=new Thread(aa,"A");
Thread B=new Thread(aa,"B");
Thread C=new Thread(aa,"C");
A.start();
B.start();
C.start();
}
};
上面每个线程都运行了10遍,
我想让它们加起来一共运行10遍,该怎么实现呢????????
{
public void run()
{
for(int a=0;a<10;a++)
{
System.out.println("线程"+Thread.currentThread().getName()+"---------"+"ABCABC.....");
}
}
};
public class apple
{
public static void main(String[] args)
{
apple1 aa=new apple1();
Thread A=new Thread(aa,"A");
Thread B=new Thread(aa,"B");
Thread C=new Thread(aa,"C");
A.start();
B.start();
C.start();
}
};
上面每个线程都运行了10遍,
我想让它们加起来一共运行10遍,该怎么实现呢????????
解决方案 »
- 求助,怎样将代码转换成pdf文件
- 一个关于poi拷贝的高级问题,请问如何实现excel的列拷贝或都是指定开始行列到结束行列的拷贝
- 取得JCheckBox选中的个数
- Swing字体
- 我才学5天这个问题难不倒你吧
- 高手进!!在网页中如何使非模式对话框 (showModelessDialog())隐藏在主窗口背后?
- 谁能帮我看看,反射的问题
- 怎样在同一Frame里建立像网页那种可以左右拉动的多个Frame?有没有StatusBar类,怎样添加状态栏?
- JAVA tcp server 管理多个客户端连接
- Java if里面的return和if外面的return到底返回的是哪个值?
- Java截取字符串
- 数据库 接口
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++;
}
}
}
};
应该在while中加个sleep,否则每次都是一个线程工作,其它得不到锁
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();
}
}
{
int T = 0; //使用共同资源
public void run()
{
//for(int a=0;a<10;a++)
while(true)
{
synchronized(this) { //使用同步
if (T >= 10) break;
System.out.println("线程"+Thread.currentThread().getName()+"---------"+"ABCABC.....");
T++;
}
}
}
};
控制下就好了