• java 多线程 数据通信


    1.司机等乘客全部上车后再启动车子

    import java.util.concurrent.CountDownLatch;

    public class A {
    public static void main(String[] args) {
    CountDownLatch countDownLatch = new CountDownLatch(3);//线程安全 内部是aqs
    //倒计数器
    for (int i = 97; i <= 100; i++) {
    String threadName = String.valueOf((char) i);
    new Thread(()->{
    if (threadName.equals("d")){
    System.out.println(threadName + "正在等待其他线程执行完毕!");
    try {
    countDownLatch.await();
    System.err.println(threadName + "执行!");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }else {
    System.out.println(threadName + "执行完啦~~~");
    countDownLatch.countDown();
    }
    }, threadName).start();
    }
    }
    }

    2.运动员准备好后通知其他运动员已准备好
    import java.util.Random;
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;

    public class B {
    public static void main(String[] args) {
    CyclicBarrier cyclicBarrier = new CyclicBarrier(3);//安全有锁
    for (int i = 97; i < 100; i++) {
    String threadName = String.valueOf((char) i);
    new Thread(() -> {
    try {
    System.out.println(threadName + "已准备!");
    cyclicBarrier.await();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (BrokenBarrierException e) {
    e.printStackTrace();
    }
    System.err.println(threadName + "执行!");
    }, threadName).start();
    }
    }
    }

    3.有返回值的

    import java.util.concurrent.*;

    public class C {
    public static void main(String[] args) {
    MyCallable callableA = new MyCallable("AAAAA");
    FutureTask<String> futureTask = new FutureTask<>(callableA);
    //-----------------------------------------------
    String ca = null;
    try {
    ca = callableA.call();//和下面的run一样
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(ca);//也会阻塞
    //-----------------------------------------------
    futureTask.run();
    try {
    String s = futureTask.get();//没有返回结果会阻塞
    System.out.println(s);
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    }
    //-----------------------------------------------
    ExecutorService threadPool= Executors.newSingleThreadExecutor();
    String plresult = null;
    try {
    plresult = threadPool.submit(callableA).get();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    } finally {
    threadPool.shutdown();
    }
    System.out.println(plresult);
    }
    }

    class MyCallable implements Callable<String>{

    private String name;

    public MyCallable(String name) {
    this.name = name;
    }

    @Override
    public String call() throws Exception {
    Thread.sleep(3000);
    return this.name;
    }
    }

    4.还有一个不知道这个是干嘛的,感觉用起来很奇怪
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;

    public class D {
    public static void main(String[] args) {
    FutureTask<String> futureTask = new FutureTask<>(new Runnable() {
    @Override
    public void run() {
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    },"上边传下边收~~不知道这是干嘛用的~!!!");
    futureTask.run();
    try {
    String s = futureTask.get();
    System.out.println(s);
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    }
    }
    }


  • 相关阅读:
    Unity NGUI 3.0.4版本 制作网络版斗地主
    unity3D与网页的交互---做项目的一点总结
    Cross-platform Tools
    Win7下Qt5的安装及使用
    VS Installer教程
    C++ & MFC
    论文模板的定制
    批处理文件
    MFC六大关键技术
    C/C++常用预处理指令
  • 原文地址:https://www.cnblogs.com/bzdofj/p/14253925.html
Copyright © 2020-2023  润新知