多線程 以及項目中的多線程

在項目中的多線程
MadAcbService類中
    
private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(500));



 //設置默認值
            Future<CheckMsg> submit = threadPoolExecutor.submit(new Callable<CheckMsg>() {
                @Override
                public CheckMsg call() throws Exception {
                    return insertWithTransactional(madAcb);
                }
            });


關於項目代碼的測試:

@Override
	public Object duoxianchengAnys() {
		//TODO 這個問題研究
		/*Local variable g defined in an enclosing scope must be final or effectively final*/
		//事實證明確實是異步的
		for(int i=1;i<20;i++){
			int g=i;
			threadPoolExecutor.submit(new Callable<CheckMsg>() {
				@Override
				public CheckMsg call() throws Exception {
					System.out.println(g);
					return null;
				}
			});
		}
		
		return null;
	}


通過輸出發現代碼確實是異步執行的

1
4
3
2
5
6
9
7
14
8
16
17
15
13
12
11
10
19
18

ThreadPoolExecutor代碼的網上的介紹:
https://www.cnblogs.com/yulinfeng/p/7021293.html
https://www.cnblogs.com/yulinfeng/p/7039979.html
發現博客園這個網址也不錯的

網上的ExecutorService方式的多線程參考網址:
https://www.cnblogs.com/jiang4yu/p/11240517.html
網址裏面還包括多線程的四種實現方式
其中的代碼在:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章