当前位置:   article > 正文

浅谈java多线程回调的三种方式适用于实际工程项目_java多线程for循环回调

java多线程for循环回调

前言

掌握同步操作,实现资源最大程度利用


目标

掌握三种回电方式
1 while循环等待
2 join
3 implements Callable

关键字

Executors,Future,get,Callable,Runnable

一、定义

以空间换时间的方式解决问题。

二、使用及场景


import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * 多线程返回值的三种方式
 * 1 while循环等待
 * 2 join
 * 3  implements Callable
 * Created by xiaojinlu1990@163.com on 2020/11/29 0029.
 */
public class ExecutorDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        //第一种 循环主线程等待法
        TestTreadFrist testTreadFrist = new TestTreadFrist();
        testTreadFrist.setNum("test1");
        Thread thread1 = new Thread();
        thread1.start();
        while (testTreadFrist.getNum()==null){
          Thread.sleep(1000);
        }
        System.out.println(testTreadFrist.getNum());
        //第二种  join等待法
        TestTreadTwe testTreadTwe = new TestTreadTwe();
        testTreadTwe.setNum("test2");
        Thread thread2 = new Thread();
        thread2.start();
        thread2.join();//join等待法
        System.out.println(testTreadTwe.getNum());
        //第三种1 实现Callable  获取返回值
        ExecutorService service = Executors.newCachedThreadPool();
        TestTreadThree testTreadThree = new TestTreadThree();
        Future<String> str = service.submit(testTreadThree);
        System.out.println(str.get());

        //第三种2 多回调返回 + 超时时间版本
        ExecutorDemo executorDemo = new ExecutorDemo();
        executorDemo.getThread();
    }

    public void  getThread() {
       ExecutorService service = Executors.newCachedThreadPool();
        ArrayList<Future<String>> results = new ArrayList<Future<String>>();
        for (int i = 0; i < 10; i++) {
           TestTread testTread = new TestTread();
           testTread.setNum(String.valueOf(i));
            results.add(service.submit(testTread));
        }
        for(Future<String> fs : results){
            try {
                System.out.println("");
                long timeOut =2;
                System.out.println(fs.get(timeOut, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                e.printStackTrace();
                return ;
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                //打印超时
                e.printStackTrace();
            } finally{
                service.shutdown();
            }
        }
    }
}

class TestTread implements Callable{
    private String num;

    public void setNum(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    @Override
    public Object call() throws Exception {
        if(num.equals("3")){

            Thread.sleep(10000);
        }else {
            Thread.sleep(1900);
        }
        System.out.printf(num);
        return getNum();
    }
}

//第一种
class TestTreadFrist implements Runnable{

    private String num;

    public void setNum(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setNum("------");

    }
}
//第二种
class TestTreadTwe implements Runnable{
    private String num;

    public void setNum(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    @Override
    public void run() {

    }
}
//第三种 实现Callable
class TestTreadThree implements Callable{
    private String num;

    public void setNum(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    @Override
    public Object call() throws Exception {
        setNum("test3");
        return getNum();
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155

总结

根据场景对用适应才最好,最适宜的就是最好的。

参考

java API
https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
多留言多点赞你们的只支持是我坚持下去的动力,都支棱起来!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49962
推荐阅读
相关标签
  

闽ICP备14008679号