import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class Main {
private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
final static CyclicBarrier barrier = new CyclicBarrier(4);
public static void main(String[] args) {
int data[] = new int[4];
for(int i=0;i<=3; i++){
final int j=i;
executor.execute(new Runnable() {
@Override
public void run() {
data[j]=j;
// System.out.println(j+"----" + Thread.currentThread().getName());
// System.out.println(data[j]);
try{
barrier.await();
}catch (Exception e){
e.printStackTrace();
}
}
});
}
// try {
// Thread.sleep(30000); //1000 毫秒,也就是1秒.
// } catch(InterruptedException ex) {
// Thread.currentThread().interrupt();
// }
try {
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int k=0;k<=3;k++){
System.out.println(data[k]);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class Main {
private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
final static CyclicBarrier barrier = new CyclicBarrier(4);
public static void main(String[] args) {
//int data[] = new int[4];
String[] s= new String[4];
for(int i=0;i<=3; i++){
final int j=i;
executor.execute(new Runnable() {
@Override
public void run() {
s[j]="aaaaaaaaa"+j;
// System.out.println(j+"----" + Thread.currentThread().getName());
// System.out.println(data[j]);
try{
barrier.await();
}catch (Exception e){
e.printStackTrace();
}
}
});
}
// try {
// Thread.sleep(30000); //1000 毫秒,也就是1秒.
// } catch(InterruptedException ex) {
// Thread.currentThread().interrupt();
// }
try {
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int k=0;k<=3;k++){
System.out.println(s[k]);
}
}
}
import java.util.concurrent.*; public class Main { public static void main(String[] args) throws InterruptedException { ExecutorService exector = Executors.newFixedThreadPool(12); int threadNumber = 10; String[] s = new String[10]; final CountDownLatch countDownLatch = new CountDownLatch(threadNumber); for (int i = 0; i < threadNumber; i++) { final int threadID = i; exector.execute( () -> { try { Thread.sleep(3000); s[threadID] = "aaaaaaaaa" + threadID; System.out.println(String.format("threadID:[%s] finished!!", threadID)); } catch (Exception e) { e.printStackTrace(); } finally { countDownLatch.countDown(); //这个不管是否异常都需要数量减,否则会被堵塞无法结束 } } ); } countDownLatch.await();//保证之前的所有的线程都执行完成,才会走下面的 System.out.println(countDownLatch.getCount()); System.out.println("main thread finished!!"); for (int k = 0; k < s.length; k++) { System.out.println(s[k]); } } }