1. 程式人生 > >對Executors.newFixedThreadPool,CountDownLatch的個人初步瞭解

對Executors.newFixedThreadPool,CountDownLatch的個人初步瞭解

Executors.newFixedThreadPool

ExecutorService是Executor直接的擴充套件介面,也是最常用的執行緒池介面,我們通常見到的執行緒池定時任務執行緒池都是它的實現類。

Executors.newFixedThreadPool一個固定大小的執行緒池

Executors.newFixedThreadPool(4)好比如一個車間裡面只能允許4個人的容量,然後4個人一起工作,對於龐大的工作量,只能夠4個人誰先完成任務再去接新任務

public class Demo4 {
	public static void main(String[] args) {
		// 第一個執行緒池的
		int a = 0;
		List<Integer> list = new ArrayList<>();
		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
		for (int i = 0; i < 10000; i++) {
			list.add(a);
			a++;
		}
		
		// 迴圈建立執行緒,最多隻能建立4個
		for (int i = 0; i < list.size(); i++) {
			int z = list.get(i);
			// 建立執行緒
			fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + " " + z);
					try {
						// 第一個執行緒休息1秒,建立的第二個執行緒也休息1秒,直到第4個也休息1s,第一個執行緒休息1秒完畢後空閒出來繼續執行迴圈,每個執行緒休息1秒後都恢復空閒狀態
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
		}

	}
}

記得關閉執行緒池,不然控制檯一直佔著執行著浪費記憶體:

fixedThreadPool2.shutdown();

輸出結果:

pool-1-thread-2 1
pool-1-thread-3 2
pool-1-thread-1 0
pool-1-thread-4 3
pool-1-thread-3 5
pool-1-thread-1 7
pool-1-thread-2 6
pool-1-thread-4 4
pool-1-thread-4 9
pool-1-thread-2 11
pool-1-thread-1 10
pool-1-thread-3 8
pool-1-thread-2 12
pool-1-thread-4 13
pool-1-thread-3 15
pool-1-thread-1 14

CountDownLatch

CountDownLatch就是一個計數器,作用就是當執行緒執行完畢通過countDown來進行減1,當初始化的值減到為0才執行主執行緒(main的程式碼,不包含在thread或者執行緒池裡面的程式碼)

先嚐試一下沒有計數器的情況:

public class Demo4 {
	public static void main(String[] args) {

		int b = 0;
		int c = 0;
		int d = 0;
		int e = 0;

		List<Integer> list2 = new ArrayList<>();
		List<Integer> list3 = new ArrayList<>();
		List<Integer> list4 = new ArrayList<>();
		List<Integer> list5 = new ArrayList<>();
		ExecutorService fixedThreadPool2 = Executors.newFixedThreadPool(4);
//		CountDownLatch lautch = new CountDownLatch(190);
		for (int i = 0; i < 100; i++) {
			list2.add(b);
			b++;
		}
		for (int i = 0; i < 50; i++) {
			list3.add(c);
			c++;
		}
		for (int i = 0; i < 30; i++) {
			list4.add(d);
			d++;
		}
		for (int i = 0; i < 10; i++) {
			list5.add(e);
			e++;
		}
		//重複利用執行緒池裡面的執行緒190次
		for (int i = 0; i < list2.size(); i++) {
			int y = list2.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是y:" + y);
//					lautch.countDown();
				}
			});
		}

		for (int i = 0; i < list3.size(); i++) {
			int x = list3.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是x:" + x);
//					lautch.countDown();
				}
			});
		}

		for (int i = 0; i < list4.size(); i++) {
			int w = list4.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是w:" + w);
//					lautch.countDown();
				}
			});
		}

		for (int i = 0; i < list5.size(); i++) {
			int v = list5.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是v:" + v);
//					lautch.countDown();
				}
			});
		}
//		try {
//			lautch.await();
//			
//		} catch (InterruptedException e1) {
//			e1.printStackTrace();
//		}
		System.out.println("開始主執行緒");
		System.out.println("主執行緒結束");
		fixedThreadPool2.shutdown();
	}
}

輸出結果:

pool-1-thread-2   我是y:1
pool-1-thread-4   我是y:3
pool-1-thread-1   我是y:0
pool-1-thread-3   我是y:2
pool-1-thread-1   我是y:6
pool-1-thread-4   我是y:5
pool-1-thread-2   我是y:4
pool-1-thread-4   我是y:9
pool-1-thread-1   我是y:8
pool-1-thread-3   我是y:7
pool-1-thread-3   我是y:13
pool-1-thread-3   我是y:14
pool-1-thread-3   我是y:15
pool-1-thread-1   我是y:12
pool-1-thread-1   我是y:17
pool-1-thread-1   我是y:18
pool-1-thread-1   我是y:19
pool-1-thread-1   我是y:20
pool-1-thread-1   我是y:21
pool-1-thread-4   我是y:11
pool-1-thread-4   我是y:23
pool-1-thread-4   我是y:24
pool-1-thread-4   我是y:25
pool-1-thread-4   我是y:26
pool-1-thread-2   我是y:10
pool-1-thread-4   我是y:27
pool-1-thread-1   我是y:22
pool-1-thread-3   我是y:16
pool-1-thread-1   我是y:30
pool-1-thread-4   我是y:29
pool-1-thread-2   我是y:28
pool-1-thread-4   我是y:33
pool-1-thread-4   我是y:35
pool-1-thread-4   我是y:36
pool-1-thread-1   我是y:32
pool-1-thread-1   我是y:38
pool-1-thread-3   我是y:31
pool-1-thread-1   我是y:39
pool-1-thread-1   我是y:41
pool-1-thread-1   我是y:42
pool-1-thread-1   我是y:43
pool-1-thread-1   我是y:44
pool-1-thread-1   我是y:45
pool-1-thread-1   我是y:46
開始主執行緒
主執行緒結束
pool-1-thread-4   我是y:37
pool-1-thread-2   我是y:34
pool-1-thread-1   我是y:47
pool-1-thread-3   我是y:40
pool-1-thread-1   我是y:50
pool-1-thread-2   我是y:49
pool-1-thread-4   我是y:48
pool-1-thread-2   我是y:53
pool-1-thread-1   我是y:52
pool-1-thread-3   我是y:51
pool-1-thread-1   我是y:56
pool-1-thread-2   我是y:55
pool-1-thread-4   我是y:54
pool-1-thread-2   我是y:59
pool-1-thread-1   我是y:58
pool-1-thread-3   我是y:57
pool-1-thread-1   我是y:62
pool-1-thread-2   我是y:61
pool-1-thread-4   我是y:60
pool-1-thread-2   我是y:65
pool-1-thread-1   我是y:64
pool-1-thread-3   我是y:63
pool-1-thread-1   我是y:68
pool-1-thread-2   我是y:67
pool-1-thread-1   我是y:70
pool-1-thread-4   我是y:66
pool-1-thread-1   我是y:72
pool-1-thread-2   我是y:71
pool-1-thread-3   我是y:69
pool-1-thread-2   我是y:75
pool-1-thread-1   我是y:74
pool-1-thread-4   我是y:73
pool-1-thread-1   我是y:78
pool-1-thread-2   我是y:77
pool-1-thread-3   我是y:76
pool-1-thread-2   我是y:81
pool-1-thread-1   我是y:80
pool-1-thread-4   我是y:79
pool-1-thread-1   我是y:84
pool-1-thread-4   我是y:85
pool-1-thread-2   我是y:83
pool-1-thread-3   我是y:82
pool-1-thread-2   我是y:88
pool-1-thread-4   我是y:87
pool-1-thread-1   我是y:86
pool-1-thread-4   我是y:91
pool-1-thread-2   我是y:90
pool-1-thread-3   我是y:89
pool-1-thread-2   我是y:94
pool-1-thread-4   我是y:93
pool-1-thread-1   我是y:92
pool-1-thread-4   我是y:97
pool-1-thread-2   我是y:96
pool-1-thread-3   我是y:95
pool-1-thread-4   我是y:99
pool-1-thread-1   我是y:98
pool-1-thread-4   我是x:2
pool-1-thread-3   我是x:1
pool-1-thread-2   我是x:0
pool-1-thread-3   我是x:5
pool-1-thread-4   我是x:4
pool-1-thread-1   我是x:3
pool-1-thread-4   我是x:8
pool-1-thread-3   我是x:7
pool-1-thread-2   我是x:6
pool-1-thread-3   我是x:11
pool-1-thread-4   我是x:10
pool-1-thread-1   我是x:9
pool-1-thread-4   我是x:14
pool-1-thread-3   我是x:13
pool-1-thread-2   我是x:12
pool-1-thread-3   我是x:17
pool-1-thread-4   我是x:16
pool-1-thread-1   我是x:15
pool-1-thread-4   我是x:20
pool-1-thread-3   我是x:19
pool-1-thread-2   我是x:18
pool-1-thread-3   我是x:23
pool-1-thread-4   我是x:22
pool-1-thread-1   我是x:21
pool-1-thread-4   我是x:26
pool-1-thread-3   我是x:25
pool-1-thread-2   我是x:24
pool-1-thread-3   我是x:29
pool-1-thread-4   我是x:28
pool-1-thread-1   我是x:27
pool-1-thread-4   我是x:32
pool-1-thread-3   我是x:31
pool-1-thread-2   我是x:30
pool-1-thread-3   我是x:35
pool-1-thread-4   我是x:34
pool-1-thread-1   我是x:33
pool-1-thread-4   我是x:38
pool-1-thread-3   我是x:37
pool-1-thread-2   我是x:36
pool-1-thread-3   我是x:41
pool-1-thread-4   我是x:40
pool-1-thread-1   我是x:39
pool-1-thread-4   我是x:44
pool-1-thread-3   我是x:43
pool-1-thread-2   我是x:42
pool-1-thread-3   我是x:47
pool-1-thread-4   我是x:46
pool-1-thread-1   我是x:45
pool-1-thread-4   我是w:0
pool-1-thread-3   我是x:49
pool-1-thread-2   我是x:48
pool-1-thread-3   我是w:3
pool-1-thread-4   我是w:2
pool-1-thread-1   我是w:1
pool-1-thread-4   我是w:6
pool-1-thread-3   我是w:5
pool-1-thread-2   我是w:4
pool-1-thread-3   我是w:9
pool-1-thread-4   我是w:8
pool-1-thread-1   我是w:7
pool-1-thread-4   我是w:12
pool-1-thread-3   我是w:11
pool-1-thread-2   我是w:10
pool-1-thread-3   我是w:15
pool-1-thread-4   我是w:14
pool-1-thread-1   我是w:13
pool-1-thread-4   我是w:18
pool-1-thread-3   我是w:17
pool-1-thread-2   我是w:16
pool-1-thread-3   我是w:21
pool-1-thread-4   我是w:20
pool-1-thread-1   我是w:19
pool-1-thread-4   我是w:24
pool-1-thread-3   我是w:23
pool-1-thread-2   我是w:22
pool-1-thread-3   我是w:27
pool-1-thread-4   我是w:26
pool-1-thread-1   我是w:25
pool-1-thread-4   我是v:0
pool-1-thread-3   我是w:29
pool-1-thread-2   我是w:28
pool-1-thread-3   我是v:3
pool-1-thread-4   我是v:2
pool-1-thread-1   我是v:1
pool-1-thread-4   我是v:6
pool-1-thread-3   我是v:5
pool-1-thread-2   我是v:4
pool-1-thread-3   我是v:9
pool-1-thread-4   我是v:8
pool-1-thread-1   我是v:7

從上面輸出結果可以看到子執行緒還沒執行完畢就被主執行緒搶佔了資源,輸出了語句

現在加上計數器:

public class Demo4 {
	public static void main(String[] args) {


		// 第二個執行緒池的
		int b = 0;
		int c = 0;
		int d = 0;
		int e = 0;

		List<Integer> list2 = new ArrayList<>();
		List<Integer> list3 = new ArrayList<>();
		List<Integer> list4 = new ArrayList<>();
		List<Integer> list5 = new ArrayList<>();
		ExecutorService fixedThreadPool2 = Executors.newFixedThreadPool(4);
		CountDownLatch lautch = new CountDownLatch(190);
		for (int i = 0; i < 100; i++) {
			list2.add(b);
			b++;
		}
		for (int i = 0; i < 50; i++) {
			list3.add(c);
			c++;
		}
		for (int i = 0; i < 30; i++) {
			list4.add(d);
			d++;
		}
		for (int i = 0; i < 10; i++) {
			list5.add(e);
			e++;
		}
		//重複利用執行緒池裡面的4個執行緒190次
		for (int i = 0; i < list2.size(); i++) {
			int y = list2.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是y:" + y);
					lautch.countDown();
				}
			});
		}

		for (int i = 0; i < list3.size(); i++) {
			int x = list3.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是x:" + x);
					lautch.countDown();
				}
			});
		}

		for (int i = 0; i < list4.size(); i++) {
			int w = list4.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是w:" + w);
					lautch.countDown();
				}
			});
		}

		for (int i = 0; i < list5.size(); i++) {
			int v = list5.get(i);
			fixedThreadPool2.execute(new Runnable() {
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName() + "   " + "我是v:" + v);
					lautch.countDown();
				}
			});
		}
		try {
			lautch.await();
			System.out.println("開始主執行緒");
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		System.out.println("主執行緒結束");
		fixedThreadPool2.shutdown();
	}
}

輸出結果:

pool-1-thread-1   我是y:0
pool-1-thread-3   我是y:2
pool-1-thread-2   我是y:1
pool-1-thread-1   我是y:4
pool-1-thread-1   我是y:7
pool-1-thread-1   我是y:8
pool-1-thread-1   我是y:9
pool-1-thread-1   我是y:10
pool-1-thread-3   我是y:5
pool-1-thread-1   我是y:11
pool-1-thread-1   我是y:13
pool-1-thread-1   我是y:14
pool-1-thread-1   我是y:15
pool-1-thread-1   我是y:16
pool-1-thread-1   我是y:17
pool-1-thread-1   我是y:18
pool-1-thread-2   我是y:6
pool-1-thread-2   我是y:20
pool-1-thread-2   我是y:21
pool-1-thread-3   我是y:12
pool-1-thread-2   我是y:22
pool-1-thread-3   我是y:23
pool-1-thread-3   我是y:25
pool-1-thread-3   我是y:26
pool-1-thread-1   我是y:19
pool-1-thread-1   我是y:28
pool-1-thread-1   我是y:29
pool-1-thread-1   我是y:30
pool-1-thread-1   我是y:31
pool-1-thread-1   我是y:32
pool-1-thread-1   我是y:33
pool-1-thread-1   我是y:34
pool-1-thread-1   我是y:35
pool-1-thread-4   我是y:3
pool-1-thread-4   我是y:37
pool-1-thread-4   我是y:38
pool-1-thread-3   我是y:27
pool-1-thread-3   我是y:40
pool-1-thread-3   我是y:41
pool-1-thread-3   我是y:42
pool-1-thread-3   我是y:43
pool-1-thread-3   我是y:44
pool-1-thread-3   我是y:45
pool-1-thread-3   我是y:46
pool-1-thread-3   我是y:47
pool-1-thread-3   我是y:48
pool-1-thread-3   我是y:49
pool-1-thread-3   我是y:50
pool-1-thread-3   我是y:51
pool-1-thread-3   我是y:52
pool-1-thread-3   我是y:53
pool-1-thread-3   我是y:54
pool-1-thread-3   我是y:55
pool-1-thread-3   我是y:56
pool-1-thread-3   我是y:57
pool-1-thread-3   我是y:58
pool-1-thread-3   我是y:59
pool-1-thread-3   我是y:60
pool-1-thread-3   我是y:61
pool-1-thread-2   我是y:24
pool-1-thread-3   我是y:62
pool-1-thread-4   我是y:39
pool-1-thread-1   我是y:36
pool-1-thread-1   我是y:66
pool-1-thread-1   我是y:67
pool-1-thread-1   我是y:68
pool-1-thread-1   我是y:69
pool-1-thread-4   我是y:65
pool-1-thread-3   我是y:64
pool-1-thread-2   我是y:63
pool-1-thread-3   我是y:72
pool-1-thread-4   我是y:71
pool-1-thread-1   我是y:70
pool-1-thread-4   我是y:75
pool-1-thread-3   我是y:74
pool-1-thread-2   我是y:73
pool-1-thread-3   我是y:78
pool-1-thread-4   我是y:77
pool-1-thread-1   我是y:76
pool-1-thread-4   我是y:81
pool-1-thread-3   我是y:80
pool-1-thread-2   我是y:79
pool-1-thread-3   我是y:84
pool-1-thread-4   我是y:83
pool-1-thread-1   我是y:82
pool-1-thread-4   我是y:87
pool-1-thread-3   我是y:86
pool-1-thread-2   我是y:85
pool-1-thread-3   我是y:90
pool-1-thread-4   我是y:89
pool-1-thread-1   我是y:88
pool-1-thread-4   我是y:93
pool-1-thread-3   我是y:92
pool-1-thread-2   我是y:91
pool-1-thread-3   我是y:96
pool-1-thread-4   我是y:95
pool-1-thread-1   我是y:94
pool-1-thread-4   我是y:99
pool-1-thread-3   我是y:98
pool-1-thread-2   我是y:97
pool-1-thread-3   我是x:2
pool-1-thread-4   我是x:1
pool-1-thread-1   我是x:0
pool-1-thread-4   我是x:5
pool-1-thread-3   我是x:4
pool-1-thread-2   我是x:3
pool-1-thread-3   我是x:8
pool-1-thread-4   我是x:7
pool-1-thread-1   我是x:6
pool-1-thread-4   我是x:11
pool-1-thread-3   我是x:10
pool-1-thread-2   我是x:9
pool-1-thread-3   我是x:14
pool-1-thread-4   我是x:13
pool-1-thread-1   我是x:12
pool-1-thread-4   我是x:17
pool-1-thread-3   我是x:16
pool-1-thread-2   我是x:15
pool-1-thread-3   我是x:20
pool-1-thread-4   我是x:19
pool-1-thread-1   我是x:18
pool-1-thread-4   我是x:23
pool-1-thread-3   我是x:22
pool-1-thread-2   我是x:21
pool-1-thread-3   我是x:26
pool-1-thread-4   我是x:25
pool-1-thread-3   我是x:28
pool-1-thread-1   我是x:24
pool-1-thread-3   我是x:30
pool-1-thread-4   我是x:29
pool-1-thread-2   我是x:27
pool-1-thread-4   我是x:33
pool-1-thread-3   我是x:32
pool-1-thread-1   我是x:31
pool-1-thread-3   我是x:36
pool-1-thread-4   我是x:35
pool-1-thread-2   我是x:34
pool-1-thread-4   我是x:39
pool-1-thread-3   我是x:38
pool-1-thread-1   我是x:37
pool-1-thread-3   我是x:42
pool-1-thread-4   我是x:41
pool-1-thread-2   我是x:40
pool-1-thread-4   我是x:45
pool-1-thread-3   我是x:44
pool-1-thread-1   我是x:43
pool-1-thread-3   我是x:48
pool-1-thread-4   我是x:47
pool-1-thread-2   我是x:46
pool-1-thread-4   我是w:1
pool-1-thread-3   我是w:0
pool-1-thread-1   我是x:49
pool-1-thread-3   我是w:4
pool-1-thread-4   我是w:3
pool-1-thread-2   我是w:2
pool-1-thread-4   我是w:7
pool-1-thread-3   我是w:6
pool-1-thread-1   我是w:5
pool-1-thread-3   我是w:10
pool-1-thread-4   我是w:9
pool-1-thread-2   我是w:8
pool-1-thread-4   我是w:13
pool-1-thread-3   我是w:12
pool-1-thread-1   我是w:11
pool-1-thread-3   我是w:16
pool-1-thread-4   我是w:15
pool-1-thread-2   我是w:14
pool-1-thread-4   我是w:19
pool-1-thread-3   我是w:18
pool-1-thread-1   我是w:17
pool-1-thread-3   我是w:22
pool-1-thread-4   我是w:21
pool-1-thread-2   我是w:20
pool-1-thread-4   我是w:25
pool-1-thread-3   我是w:24
pool-1-thread-1   我是w:23
pool-1-thread-3   我是w:28
pool-1-thread-4   我是w:27
pool-1-thread-2   我是w:26
pool-1-thread-3   我是v:0
pool-1-thread-4   我是v:1
pool-1-thread-1   我是w:29
pool-1-thread-4   我是v:4
pool-1-thread-3   我是v:3
pool-1-thread-2   我是v:2
pool-1-thread-3   我是v:7
pool-1-thread-4   我是v:6
pool-1-thread-1   我是v:5
pool-1-thread-3   我是v:9
pool-1-thread-2   我是v:8
開始主執行緒
主執行緒結束

由輸出結果可看出都是執行完子執行緒,計數完畢後才往下執行的。