请教如下方式是否可以等到所有Thread都结束后牙再往下运行
SimpleThread p1=new SimpleThread(result,al1);
p1.start();
SimpleThread p2=(new SimpleThread(result,al2));
p2.start();
SimpleThread p3=(new SimpleThread(result,al3));
p3.start();
SimpleThread p4=(new SimpleThread(result,al4));
p4.start();
SimpleThread p5=(new SimpleThread(result,al5));
p5.start();
SimpleThread p6=(new SimpleThread(result,al6));
p6.start();
SimpleThread p7=(new SimpleThread(result,al7));
p7.start();
SimpleThread p8=(new SimpleThread(result,al8));
p8.start();
SimpleThread p9=(new SimpleThread(result,al9));
p9.start();
SimpleThread p10=(new SimpleThread(result,al10));
p10.start();
try {
p1.join();
p2.join();
p3.join();
p4.join();
p5.join();
p6.join();
p7.join();
p8.join();
p9.join();
p10.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
SimpleThread p1=new SimpleThread(result,al1);
p1.start();
SimpleThread p2=(new SimpleThread(result,al2));
p2.start();
SimpleThread p3=(new SimpleThread(result,al3));
p3.start();
SimpleThread p4=(new SimpleThread(result,al4));
p4.start();
SimpleThread p5=(new SimpleThread(result,al5));
p5.start();
SimpleThread p6=(new SimpleThread(result,al6));
p6.start();
SimpleThread p7=(new SimpleThread(result,al7));
p7.start();
SimpleThread p8=(new SimpleThread(result,al8));
p8.start();
SimpleThread p9=(new SimpleThread(result,al9));
p9.start();
SimpleThread p10=(new SimpleThread(result,al10));
p10.start();
try {
p1.join();
p2.join();
p3.join();
p4.join();
p5.join();
p6.join();
p7.join();
p8.join();
p9.join();
p10.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
解决方案 »
- 为什么使用mouseListener监听就会错误呢?(我已经重写了它里面的方法)
- java程序检测不到串口
- 问个关于 流 末尾的问题
- 我们公司现在需要做C/S模式的软件,请问Java好还是.NET好?
- 求substance的教程
- 一个关于boolean的简单疑问
- 如何写SQL语句查允许空的记录
- 我写的像数据库提交图片的程序,但是不成功,强各位大虾帮忙分析一下,谢谢!!
- 哪里可以找到 jbuilder 5 中的 jDataStore 的 license serial number 和 key
- 新人学java,有一些语法的问题请教大神。
- 编译不过啊
- 初学JAVA SE,弱弱的问一下,日期和时间的转换符信息在API的哪部分?
static int num = 0;
重写start()方法
public void start(){
start();
num++;
}在每次join()之前判断一下num,如果=10就join();如果!=10就不执行。这个num就是信号量。
定义没找过,个人感觉不是他。
public TestCountDownLatch(final CountDownLatch sign, int n){
threads = new Thread[n];
for(int i=0;i<n;i++)
{
threads[i] = new Thread(new Runnable(){ @Override
public void run() {
System.out.println("work");
sign.countDown();
}
});
threads[i].start();
}
} public static void main(String[] args) {
CountDownLatch sign = new CountDownLatch(10);
new TestCountDownLatch(sign, 10);
try {
sign.await();
System.out.println("Main exit");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
信号量技术,关键类Semaphore
import java.util.concurrent.Semaphore;public class Test
{
public static void main(String[] args)
{
Semaphore semaphore = new Semaphore(0);
for (int i = 0; i < 10; i++)
{
new TestThread(i, semaphore).start();
}
try
{
semaphore.acquire(10); //需要10个信号量才能继续
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("finished!");
}
}class TestThread extends Thread
{
private int i;
private Semaphore semaphore;
public TestThread(int i, Semaphore semaphore)
{
this.i = i;
this.semaphore = semaphore;
}
public void run()
{
try
{
Thread.sleep(1000);
System.out.println("线程" + i + "结束");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
semaphore.release(); //每结束一个线程,释放一个信号量
}
}