java并发测试代码
import java.util.concurrent.CountDownLatch;
public class ThreadTest {
public static void test(int count) {
final CountDownLatch cdl=new CountDownLatch(count);
for (int i = 0; i < count; i++) {
new Thread(new Runnable() {
@Override
public void run() {
cdl.countDown();
try {
cdl.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
//并发操作
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
public static void main(String[] args) {
//10个线程并发执行
test(10);
}
}