相关疑难解决方法(0)

ScheduledExecutorService - 检查计划任务是否已完成

我有一个服务器端应用程序,客户端可以请求重新加载配置.如果客户端请求重新加载配置,则不应立即执行此操作,但延迟时间为1分钟.如果另一个客户端也请求在同一分钟重新加载配置,则应忽略此请求.

我的想法是使用ScheduledExecutorService安排任务,如:

 ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
 service.schedule(new LoadConfigurationTask(), 1, TimeUnit.MINUTES);

 public class LoadConfigurationTask Runnable {
    public void run() {
      // LoadConfiguration
    }
 }
Run Code Online (Sandbox Code Playgroud)

如何检查LoadConfigurationTask是否已被调度但尚未执行,以便能够在重新加载配置之前忽略其他请求?

java singleton multithreading timer

4
推荐指数
2
解决办法
1万
查看次数

何时使用 thread.interrupt() 以及何时抛出 InterruptedException

我有一些使用Java中断机制来完成我的工作的经验,但目前我不太清楚我应该什么时候设置当前线程的中断状态,什么时候应该抛出InterruptedException?

而且,为了让您更清楚,这里是我之前编写的示例。

这是我开始工作之前的代码:

/*
 * Run a command which locates on a certain remote machine, with the
 * specified timeout in milliseconds.
 * 
 * This method will be invoked by means of
 *     java.util.concurrent.FutureTask
 * which will further submmited to a dedicated
 *     java.util.concurrent.ExecutorService
 */
public void runRemoteSript(String command, long timeout) {
    Process cmd = null;
    try {
        cmd = Runtime.getRuntime().exec(command);

        boolean returnCodeOk = false;
        long endTime = System.currentTimeMillis() + timeout;

        // wait for the command to complete
        while (!returnCodeOk …
Run Code Online (Sandbox Code Playgroud)

java multithreading interrupt-handling

3
推荐指数
1
解决办法
5034
查看次数

为什么我必须在try/catch语句中包装每个Thread.sleep()调用?

我正在尝试用Java编写我的第一个多线程程序.我无法理解为什么我们需要围绕for循环进行此异常处理.当我在没有try/catch子句的情况下编译时,它会产生InterruptedException.(这是消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type InterruptedException
Run Code Online (Sandbox Code Playgroud)

)

但是当使用try/catch运行时,catch块中的sysout永远不会显示 - 这意味着没有捕获到这样的异常!

public class SecondThread implements Runnable{
    Thread t;

    SecondThread(){
        t = new Thread(this, "Thread 2");
        t.start();
    }

    public void run(){
        try{
            for(int i=5 ; i>0 ; i--){
                System.out.println("thread 2: " + i);
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException e){
            System.out.println("thread 2 interrupted");
        }
    }
}


public class MainThread{

    public static void main(String[] args){
        new SecondThread();

        try{
            for(int i=5 ; i>0 ; i--){
                System.out.println("main thread: …
Run Code Online (Sandbox Code Playgroud)

java interrupted-exception interruption

2
推荐指数
1
解决办法
9946
查看次数

如何实现阻塞线程池执行器?

我们有一个大文本文件,其中每一行都需要密集的process. 设计是让 aclass读取文件并将每一行的处理委托给 a thread, via thread pool。一旦池中没有空闲线程来进行处理,就应该阻止文件读取器类读取下一行。所以我需要一个blocking thread pool

在当前的实现ThreadPoolExecutor.submit()ThreadPoolExecutor.execute()方法中RejectedExecutionException,在配置的线程数变得忙碌之后抛出异常,如下面的代码片段所示。

public class BlockingTp {

    public static void main(String[] args) {
        BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        ThreadPoolExecutor executorService=
            new ThreadPoolExecutor(1, 3, 30, TimeUnit.SECONDS, blockingQueue);
        int Jobs = 10;
        System.out.println("Starting application with " + Jobs + " jobs");
        for (int i = 1; i <= Jobs; i++)
            try {
                executorService.submit(new WorkerThread(i));
                System.out.println("job added " + (i));
            } catch …
Run Code Online (Sandbox Code Playgroud)

java multithreading threadpoolexecutor

2
推荐指数
1
解决办法
3860
查看次数

Java线程和线程中断

从我可以整合的所有资源中,我仍然无法掌握线程中断.从Oracle自己的教程(https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html)中,他们没有解释什么原因导致中断异常被抛出,或者是如何,甚至是什么......他们只是说每当你调用Thread.sleep()之类的某些方法时就会发生这种情况.它被称为随机发生吗?是否有一段时间抛出异常?它每次被调用时都被抛出?这一切都没有意义,因为如果它是随机的,它对程序员来说是无用的,并且如果它在每次调用某个方法时发生,它就没有实际用途......它们对它的解释很少,只是移动学习C,Lua或Python时,我没有遇到任何问题,因为他们提供的教程和文档实际上解释了一切,这是我第一次被教程完全困扰.我一直在寻找约2个小时的答案.

你可以包括InterruptedException究竟是什么,以及为什么抛出它?

java multithreading

1
推荐指数
1
解决办法
1167
查看次数

Java 中的 InterruptedException 是什么?

我在代码中多次看到出现中断异常。我如何解决它?

例子:

for (int i = 0; i < readLimit; i++) {
    if (fileName.exists())
        return readFile(fileName);
        Thread.sleep(1000); // Here is where I get the error
    }
}
Run Code Online (Sandbox Code Playgroud)

java android interrupted-exception

0
推荐指数
1
解决办法
2661
查看次数