woxiangjingjing
V2EX  ›  Java

多线程问题

  •  
  •   woxiangjingjing · Aug 6, 2019 · 5216 views
    This topic created in 2646 days ago, the information mentioned may be changed or developed.

    多线程情况下,根据业务唯一业务 id 查询 a,b,c 三个表,并且将三个表数据集成起来怎么显示

    15 replies  •  2019-08-16 09:44:13 +08:00
    1424659514
    1424659514
    Aug 6, 2019
    1
    emmmmmm, 只是查询的话跟多线程没太大关系呀
    snappyone
    snappyone
    Aug 6, 2019
    2
    Countdownlatch 并发查询,然后代码里面合并数据
    dovme
    dovme
    Aug 6, 2019
    3
    @snappyone #2 +1
    zardly666
    zardly666
    Aug 6, 2019
    4
    线程用 Callable+Future 返回数据,三个线程都返回值以后在 Countdownlatch 倒计时完毕后 把数据合并起来
    NoOneAI
    NoOneAI
    Aug 6, 2019
    5
    我一般用 LinsteningExecutorService 和 CountDownLatch 结合起来做,在 onSuccess 和 onFailure 里面使用 countDown()方法,这样便于捕获到异常情况。
    woxiangjingjing
    woxiangjingjing
    OP
    Aug 6, 2019
    6
    @zardly666 countdownlatch 是在哪用啊,这种写对不对
    public class test {



    public static void main(String[] args) {
    ExecutorService executor = Executors.newCachedThreadPool();
    A a = new A();
    B b = new B();
    C c = new C();

    Future<Integer> result = executor.submit(a);
    Future<Integer> result1 = executor.submit(b);
    Future<Integer> result2 = executor.submit(c);
    executor.shutdown();

    try {
    Thread.sleep(1000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }

    System.out.println("主线程在执行任务");

    try {
    System.out.println("task 运行结果"+result.get()+result1.get()+result2.get());
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    }

    System.out.println("所有任务执行完毕");
    }
    }

    class A implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
    System.out.println("查询 A 表数据");
    Thread.sleep(3000);
    int sum = 0;
    for(int i=0;i<100;i++)
    sum += i;
    return sum;
    }
    }
    class B implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
    System.out.println("查询 B 表数据");
    Thread.sleep(3000);
    int sum = 0;
    for(int i=0;i<100;i++)
    sum += i;
    return sum;
    }
    }
    class C implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
    System.out.println("查询 C 表数据");
    Thread.sleep(3000);
    int sum = 0;
    for(int i=0;i<100;i++)
    sum += i;
    return sum;
    }
    }
    lihongjie0209
    lihongjie0209
    Aug 6, 2019
    7
    前端的 promise java 中的 completeableFuture
    zardly666
    zardly666
    Aug 6, 2019
    8
    @woxiangjingjing CountDownLatch 是一个类,要传参数


    import java.util.concurrent.*;
    public class Count {
    public static void main(String[] args) {
    ExecutorService executor = Executors.newCachedThreadPool();
    //定时器要传参数(几个线程完成以后再执行主线程)
    CountDownLatch latch = new CountDownLatch(3);
    //每个线程要把 CountDownLatch 传进去,每个线程完成以后倒计时-1
    A a = new A(latch);
    B b = new B(latch);
    C c = new C(latch);

    Future<Integer> result = executor.submit(a);
    Future<Integer> result1 = executor.submit(b);
    Future<Integer> result2 = executor.submit(c);

    executor.shutdown();
    //3 个线程都完成以后就会执行主线程
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }

    System.out.println("主线程在执行任务");

    try {
    System.out.println("task 运行结果"+result.get()+result1.get()+result2.get());
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    }
    System.out.println("所有任务执行完毕");

    }
    }

    import java.util.concurrent.Callable;
    import java.util.concurrent.CountDownLatch;

    public class A implements Callable<Integer>{
    CountDownLatch latch;
    public A(CountDownLatch latch) {
    this.latch = latch;
    }

    @Override
    public Integer call() throws Exception {
    System.out.println("查询 A 表数据");
    Thread.sleep(3000);
    int sum = 0;
    for(int i=0;i<100;i++)
    sum += i;
    latch.countDown();
    return sum;
    }
    }

    你的 CountDownLatch 用的不对,再看看相关知识吧~
    zardly666
    zardly666
    Aug 6, 2019
    9
    还有求和 相加运算括起来,不然输出的是字符串相加了就成 495049504950 了
    System.out.println("task 运行结果"+(result.get()+result1.get()+result2.get()));


    latch.countDown();
    每个线程中的倒计时减少是这个
    l8g
    l8g
    Aug 6, 2019❤️1
    10
    @zardly666 你这用的也不对吧

    另外楼主这个场景直接用 Future 就行了,没有再使用 CountDownLatch 的必要吧。
    zardly666
    zardly666
    Aug 6, 2019❤️1
    11
    @l8g 哈哈是的 我打上去也发现了- - 没必要 CountDownLatch 直接 Future 就完事辽=、= 我看楼上说了个 CountDownLatch 想偏了
    zardly666
    zardly666
    Aug 6, 2019
    12
    @woxiangjingjing 帮我删一下我的回复=。= 谢谢~ 我那个不对,直接多线程 Future 带回来处理就行了
    woxiangjingjing
    woxiangjingjing
    OP
    Aug 6, 2019
    13
    @zardly666 @l8g 假如相关联的书籍达到几十万条的时候,合并的时候怎么合并,是有 jdk8 的 stream 和 lamada 吗,还有没有别的解决方案没
    aguesuka
    aguesuka
    Aug 7, 2019 via Android
    14
    用 fork/join
    anakinsky
    anakinsky
    Aug 16, 2019
    15
    CompletableFuture.supplyAsync,然后 join 聚合
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1408 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 20:49 · PVG 04:49 · LAX 12:49 · JFK 15:49
    ♥ Do have faith in what you're doing.