热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

多线程的几个方法

为什么80%的码农都做不了架构师?packageapiDemo;importjava.util.Random;importjava.util.concurre

为什么80%的码农都做不了架构师?>>>   hot3.png

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 callable &#61; new Callable() {&#64;Overridepublic Integer call() throws Exception {System.out.println("Task starts");Thread.sleep(1000);int result &#61; 0;for (int i&#61;0;i<&#61;100;i&#43;&#43;){result &#43;&#61; 1;}System.out.println("Task finished and return result");return result;}};FutureTask futureTask &#61; new FutureTask(callable);new Thread(futureTask).start();try {System.out.println("Before futureTask.get()");System.out.println("Result: "&#43; futureTask.get());System.out.println("After futureTask.get()");} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}/*** 结果&#xff1a;* Before futureTask.get()Task startsTask finished and return resultResult: 101After futureTask.get()*//*** 将目标字符串打印出来* &#64;param threadName*/private static void printNumber(String threadName){int i &#61; 0;while (i&#43;&#43; <3){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(threadName &#43; "print" &#43; i);}}public static void main(String[] args) {
// demo1();
// demo2();
// demo3();
// runDAfterABC();
// runABCWhenAllReady();doTaskWithResultInWorker();}
}

 


转:https://my.oschina.net/ayo123/blog/1572808



推荐阅读
author-avatar
不如藏拙_487
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有