为什么80%的码农都做不了架构师?>>>
package apiDemo;import java.util.Random;
import java.util.concurrent.*;/*** anyou*/
public class newDemo {/*** 让两个线程依次执行*/private static void demo1(){Thread A &#61; new Thread(new Runnable() {&#64;Overridepublic void run() {printNumber("A");}});Thread B &#61; new Thread(new Runnable() {&#64;Overridepublic void run() {printNumber("B");}});A.start();B.start();}/*** 结果&#xff1a;* Bprint1Aprint1Aprint2Bprint2Bprint3Aprint3*//*** B在A执行完之后再执行*/private static void demo2(){final Thread A &#61; new Thread(new Runnable() {&#64;Overridepublic void run() {printNumber("A");}});Thread B &#61; new Thread(new Runnable() {&#64;Overridepublic void run() {System.out.println("B 开始等待 A");try {A.join();} catch (InterruptedException e) {e.printStackTrace();}printNumber("B");}});B.start();A.start();}/*** 结果&#xff1a;* B 开始等待 AAprint1Aprint2Aprint3Bprint1Bprint2Bprint3*//*** A和B交叉运行*/private static void demo3(){final Object lock &#61; new Object();Thread A &#61; new Thread(new Runnable() {&#64;Overridepublic void run() {synchronized (lock) {System.out.println("A 1");try {System.out.println("A is waiting!");lock.wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("A 2");System.out.println("A 3");}}});Thread B &#61; new Thread(new Runnable() {&#64;Overridepublic void run() {synchronized (lock) {System.out.println("B 1");System.out.println("B 2");System.out.println("B 3");lock.notify();}}});A.start();B.start();}/*** 结果&#xff1a;* A 1A is waiting!B 1B 2B 3A 2A 3*//*** 四个线程 A B C D&#xff0c;其中 D 要等到 A B C 全执行完毕后才执行&#xff0c;而且 A B C 是同步运行的*/private static void runDAfterABC(){int worker &#61; 3;final CountDownLatch countDownLatch &#61; new CountDownLatch(worker);new Thread(new Runnable() {&#64;Overridepublic void run() {System.out.println("D is waiting for other three threads!");try {countDownLatch.await();System.out.println("All done, D starts working");} catch (InterruptedException e) {e.printStackTrace();}}}).start();for (char threadName &#61; &#39;A&#39;; threadName <&#61; &#39;C&#39;; threadName &#43;&#43;){final String tN &#61; String.valueOf(threadName);new Thread(new Runnable() {&#64;Overridepublic void run() {System.out.println(tN &#43;" is working");try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(tN &#43; " finished");countDownLatch.countDown();}}).start();}}/*** 结果&#xff1a;* D is waiting for other three threads!A is workingB is workingC is workingA finishedB finishedC finishedAll done, D starts working*//*** 三个运动员各自准备&#xff0c;等到三个人都准备好后&#xff0c;再一起跑*/private static void runABCWhenAllReady(){int runner &#61; 3;final CyclicBarrier cyclicBarrier &#61; new CyclicBarrier(runner);final Random random &#61; new Random();for (char runnerName &#61; &#39;A&#39;; runnerName <&#61; &#39;C&#39;;runnerName&#43;&#43;){final String rN &#61; String.valueOf(runnerName);new Thread(new Runnable() {&#64;Overridepublic void run() {long prepareTime &#61; random.nextInt(10000) &#43; 100;System.out.println(rN &#43; " is preparing for time: "&#43;prepareTime);try {Thread.sleep(prepareTime);} catch (InterruptedException e) {e.printStackTrace();}try {System.out.println(rN &#43; "is prepared, waiting for others");cyclicBarrier.await();} catch (InterruptedException e) {e.printStackTrace();} catch (BrokenBarrierException e) {e.printStackTrace();}System.out.println(rN &#43; "starts running");}}).start();}}/*** 结果&#xff1a;* A is preparing for time: 5592B is preparing for time: 2906C is preparing for time: 5513Bis prepared, waiting for othersCis prepared, waiting for othersAis prepared, waiting for othersAstarts runningBstarts runningCstarts running*//*** 子线程完成某件任务后&#xff0c;把得到的结果回传给主线程*/private static void doTaskWithResultInWorker(){Callable
// demo1();
// demo2();
// demo3();
// runDAfterABC();
// runABCWhenAllReady();doTaskWithResultInWorker();}
}