Java 倒计时锁don';不要停止数到零
我有一个我希望打印值的代码示例。正如我所认为的那样,在countDownLatch.countDown()之后运行方法;被称为CountDownLatch的应该为零,main方法应该终止,但它没有发生。我错过什么了吗Java 倒计时锁don';不要停止数到零,java,multithreading,countdownlatch,Java,Multithreading,Countdownlatch,我有一个我希望打印值的代码示例。正如我所认为的那样,在countDownLatch.countDown()之后运行方法;被称为CountDownLatch的应该为零,main方法应该终止,但它没有发生。我错过什么了吗 public class ExecutorWithCountDownLatch { public static void main(String[] args) throws InterruptedException { final Ex
public class ExecutorWithCountDownLatch {
public static void main(String[] args) throws InterruptedException {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
final CountDownLatch countDownLatch = new CountDownLatch(1);
executorService.execute(new ThreadCounter(countDownLatch));
countDownLatch.await(5, TimeUnit.SECONDS);
}
private static class ThreadCounter implements Runnable {
private CountDownLatch countDownLatch;
public ThreadCounter(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 0; i <= 25; i++) {
System.out.println("printing numbers: " + i);
}
countDownLatch.countDown();
}
}
}
带有倒计时锁存器的公共类执行器{
公共静态void main(字符串[]args)引发InterruptedException{
final ExecutorService ExecutorService=Executors.newSingleThreadExecutor();
最终倒计时锁存器倒计时锁存器=新倒计时锁存器(1);
executorService.execute(新线程计数器(countDownLatch));
倒计时锁存。等待(5,时间单位。秒);
}
私有静态类ThreadCounter实现可运行{
私人倒计时锁存器倒计时锁存器;
公共线程计数器(CountDownLatch CountDownLatch){
this.countDownLatch=countDownLatch;
}
@凌驾
公开募捐{
对于(int i=0;i您需要在作业完成后关闭ExecutorService
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate
* threads that the caller is not permitted to modify
* because it does not hold {@link
* java.lang.RuntimePermission}{@code ("modifyThread")},
* or the security manager's {@code checkAccess} method
* denies access.
*/
void shutdown();
要强制关闭,请使用ExecutorService.shutdownNow();
:
/**
*尝试停止所有正在执行的任务,停止
*处理等待的任务,并返回任务列表
*他们正在等待处决。
*
*此方法不会等待主动执行的任务完成
*终止。使用{@link#waittermination waittermination}来
*那样做。
*
*除了尽最大努力尝试停止之外,没有其他保证
*处理积极执行的任务。例如,典型
*实现将通过{@link Thread#interrupt}取消,因此
*无法响应中断的任务可能永远不会终止。
*
*@返回从未开始执行的任务列表
*@在存在安全管理器且
*关闭此服务可能会操纵
*不允许调用方修改的线程
*因为它没有{@link
*java.lang.RuntimePermission}{@code(“modifyThread”)},
*或者安全管理器的{@code checkAccess}方法
*拒绝访问。
*/
列出shutdownNow();
作业完成后,您需要关闭执行器服务
/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate
* threads that the caller is not permitted to modify
* because it does not hold {@link
* java.lang.RuntimePermission}{@code ("modifyThread")},
* or the security manager's {@code checkAccess} method
* denies access.
*/
void shutdown();
要强制关闭,请使用ExecutorService.shutdownNow();:
/**
*尝试停止所有正在执行的任务,停止
*处理等待的任务,并返回任务列表
*他们正在等待处决。
*
*此方法不会等待主动执行的任务完成
*终止。使用{@link#waittermination waittermination}来
*那样做。
*
*除了尽最大努力尝试停止之外,没有其他保证
*处理积极执行的任务。例如,典型
*实现将通过{@link Thread#interrupt}取消,因此
*无法响应中断的任务可能永远不会终止。
*
*@返回从未开始执行的任务列表
*@在存在安全管理器且
*关闭此服务可能会操纵
*不允许调用方修改的线程
*因为它没有{@link
*java.lang.RuntimePermission}{@code(“modifyThread”)},
*或者安全管理器的{@code checkAccess}方法
*拒绝访问。
*/
列出shutdownNow();
/**
* Attempts to stop all actively executing tasks, halts the
* processing of waiting tasks, and returns a list of the tasks
* that were awaiting execution.
*
* <p>This method does not wait for actively executing tasks to
* terminate. Use {@link #awaitTermination awaitTermination} to
* do that.
*
* <p>There are no guarantees beyond best-effort attempts to stop
* processing actively executing tasks. For example, typical
* implementations will cancel via {@link Thread#interrupt}, so any
* task that fails to respond to interrupts may never terminate.
*
* @return list of tasks that never commenced execution
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate
* threads that the caller is not permitted to modify
* because it does not hold {@link
* java.lang.RuntimePermission}{@code ("modifyThread")},
* or the security manager's {@code checkAccess} method
* denies access.
*/
List<Runnable> shutdownNow();