对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
开始主线程
主线程结束

由输出结果可看出都是执行完子线程,计数完毕后才往下执行的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ExecutorService是Java中用于管理线程池的接口,Executors类提供了一些静态方法来创建不同类型的线程池。在这个例子中,通过调用Executors.newFixedThreadPool(5)方法创建了一个固定大小为5的线程池。 接下来,通过executorService.submit方法提交了20个任务。submit方法会返回一个Future对象,可以用来获取任务的执行结果。 要实现同步执行这20个任务,可以使用CountDownLatch来实现。CountDownLatch是一个同步辅助类,它可以让一个或多个线程等待其他线程完成操作后再继续执行。 下面是实现的代码示例: ```java import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Main { public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(5); CountDownLatch latch = new CountDownLatch(20); for (int i = 0; i < 20; i++) { final int taskNumber = i; executorService.submit(() -> { // 执行任务的代码 System.out.println("Task " + taskNumber + " executed"); latch.countDown(); }); } latch.await(); // 等待所有任务完成 executorService.shutdown(); // 关闭线程池 } } ``` 在上面的代码中,通过CountDownLatch来等待所有任务完成。每个任务执行完毕后,调用latch.countDown()方法减少计数器的值。在主线程中,调用latch.await()方法来等待计数器变为0,即所有任务完成后再继续执行后续代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值