JDK1.5有几个很重要的线程帮助类,对处理线程序很有帮助,他们分别是
CountDownLatch ,Executor ,Executors,下面是他们的简单用法
package com.tang.test;
import java.util.concurrent.CountDownLatch;
public class Job implements Runnable {
private final CountDownLatch doneSignal;
private final String name;
Job(CountDownLatch doneSignal, String name) {
this.doneSignal = doneSignal;
this.name = name;
}
public void run() {
try {
System.out.println(name + " begin to do his work ");
Thread.sleep(2000);//等待2秒钟,可以理解为工作的时间
System.out.println(name + " have done his work ");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
doneSignal.countDown();//记数器-1
}
}
}
package com.tang.test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class DoWork {
private static final int N = 5;
public static void main(String[] args) {
CountDownLatch doneSignal = new CountDownLatch(5);//记数器值为5
Executor e = Executors.newFixedThreadPool(2);//执行者数值为1
for (int i = 0; i < N; ++i)
e.execute(new Job(doneSignal, " worker: "+i));//开始执行任务
try {
doneSignal.await();//等待计数器中所有任务完成
System.out.println("all work have done!");
} catch (InterruptedException e1) {
e1.printStackTrace();
}finally{
System.exit(0);//关闭程序
}
}
}
输出结果
worker: 0 begin to do his work
worker: 1 begin to do his work
worker: 0 have done his work
worker: 1 have done his work
worker: 2 begin to do his work
worker: 3 begin to do his work
worker: 3 have done his work
worker: 4 begin to do his work
worker: 2 have done his work
worker: 4 have done his work
all work have done!
修改执行者数目为5, 输出结果为:
worker: 0 begin to do his work
worker: 3 begin to do his work
worker: 1 begin to do his work
worker: 2 begin to do his work
worker: 4 begin to do his work
worker: 4 have done his work
worker: 3 have done his work
worker: 1 have done his work
worker: 2 have done his work
worker: 0 have done his work
all work have done!
修改执行者数为8,输出结果为:
worker: 0 begin to do his work
worker: 2 begin to do his work
worker: 4 begin to do his work
worker: 1 begin to do his work
worker: 3 begin to do his work
worker: 2 have done his work
worker: 4 have done his work
worker: 3 have done his work
worker: 0 have done his work
worker: 1 have done his work
all work have done!
分享到:
相关推荐
在Java并发编程中,`CountDownLatch`是一个同步辅助类,它允许多个线程等待其他线程完成操作。在批量插入数据的场景下,可以创建一个CountDownLatch对象,初始化为线程的数量,每个线程处理完自己的数据后调用`...
在Java多线程编程中,CountDownLatch是一个非常重要的同步工具类,它可以帮助我们协调多个线程之间的交互。本文将深入探讨CountDownLatch的工作原理、使用场景以及相关源码分析。 CountDownLatch是一个计数器,初始...
`CountDownLatch` 是一个计数器,可以用于协调多个线程间的活动,等待所有线程完成各自的任务后,主线程或其他线程才能继续执行。 首先,让我们深入理解 `CountDownLatch` 的工作原理。`CountDownLatch` 在构造时会...
Java中的CountDownLatch是一种多线程同步工具类,它在并发编程中扮演着重要角色,尤其在需要等待一组任务完成后再进行下一步操作的场景下。CountDownLatch的命名来源于其功能,"count down"意味着计数器向下递减,...
CountDownLatch 是 Java 中的一个同步工具类,允许一个或多个线程等待其他线程完成操作。它的应用场景非常广泛,例如在处理大量数据时,可以使用多线程的方式处理,然后在主线程等待所有子线程处理完成。 ...
Java中的CountDownLatch是一种多线程同步工具类,它允许一个或多个线程等待其他线程完成操作。在Java多线程编程中,CountDownLatch扮演着“共享锁”的角色,类似于读写锁中的ReadLock,其核心功能是通过一个计数器来...
总的来说,Java的CountDownLatch是实现多线程同步的有效工具,它可以帮助开发者在复杂并发场景下控制线程的执行流程,确保程序的正确性和一致性。在实际开发中,根据需求选择合适的同步工具,如Semaphore(信号量)...
`CountDownLatch`是Java并发包(java.util.concurrent)中的一个计数器类,它允许一个或多个线程等待其他线程完成操作。在初始化时,`CountDownLatch`需要一个非负整数作为计数器的初始值。每次调用`countDown()`...
总结来说,CountDownLatch和CyclicBarrier都是Java并发编程中的重要工具,它们可以帮助开发者协调多线程间的同步行为。CountDownLatch更适合于一次性使用的场景,用于让一个线程等待其他线程完成特定操作,而...
《JAVA多线程教学演示系统》是一篇深入探讨JAVA多线程编程的论文,它针对教育领域中的教学需求,提供了一种生动、直观的演示方式,帮助学生更好地理解和掌握多线程技术。这篇论文的核心内容可能包括以下几个方面: ...
本资源包含的"多线程相关代码(V3)"提供了关于多线程编程的一些示例,涵盖了Lock、synchronized、Join、ThreadLocal、Executors以及CountDownLatch等多个关键概念。下面我们将详细探讨这些知识点。 1. **Lock接口*...
1. CountDownLatch:用于一次性阻塞多个线程,直到计数器归零才释放。 2. CyclicBarrier:允许多个线程等待至某个屏障点,然后一起继续执行。 3. Semaphore:信号量,用于限制同时访问某个资源的线程数量。 4. ...
本书还涉及到了Java并发工具类,如Semaphore信号量、CyclicBarrier栅栏、CountDownLatch倒计时器等,这些都是Java并发编程中的重要工具,可以帮助开发者更精细地控制线程执行。另外,书中还会介绍如何使用Future和...
Java中的`volatile`关键字、`Atomic`类和`ThreadLocal`变量可以帮助我们在多线程环境下正确地管理共享状态,防止数据不一致和竞态条件。 最后,性能优化也是多线程编程的重要一环。通过合理调度线程、避免不必要的...
JAVA多线程CountDownLatch使用详解 JAVA多线程CountDownLatch是JAVA多线程编程中的一种同步工具,主要用来让某个线程等待其他线程执行完毕后再继续执行。下面我们将详细介绍JAVA多线程CountDownLatch的使用和原理。...
1. java.util.concurrent包下的工具类,如CountDownLatch、CyclicBarrier、Semaphore等,用于协调多个线程之间的操作。 以上内容只是《Java多线程编程核心技术》教程中的一部分核心知识点,实际学习中还需要结合...
- **创建线程池**:通过`ExecutorService`接口实例化,如`Executors`类提供的`newFixedThreadPool`、`newCachedThreadPool`等静态方法。 - **提交任务**:使用`ExecutorService`的`execute()`方法提交`Runnable`或...
**并发工具类**:Java并发包(`java.util.concurrent`)提供了一系列高级并发工具,如`Semaphore`(信号量)、`CyclicBarrier`(循环屏障)、`CountDownLatch`(倒计时器)和`ExecutorService`等,它们可以帮助开发者...
Java并发核心编程涉及到的概念有线程状态、上下文切换、并发容器(如`ConcurrentHashMap`、`CopyOnWriteArrayList`)以及并发工具类(如`CountDownLatch`、`CyclicBarrier`、`Semaphore`)。这些工具帮助开发者有效...
9. **并发工具类**:`CountDownLatch`、`CyclicBarrier`和`Semaphore`等并发工具类可以帮助控制线程的启动和同步,提高多线程操作的协调性。 10. **安全性**:在多线程环境下,文件操作需注意文件锁,避免同一时刻...