相关疑难解决方法(0)

我怎么能杀死一个线程?不使用stop();

Thread currentThread=Thread.currentThread();
        public void run()
        {               

             while(!shutdown)
            {                               
                try
                {
                    System.out.println(currentThread.isAlive());
                Thread.interrupted();

                System.out.println(currentThread.isAlive());
                if(currentThread.isAlive()==false)
                {
                    shutdown=true;
                }
                }
                catch(Exception e)
                {
                    currentThread.interrupt();
                }                   
            }
        }

    });
    thread.start();
Run Code Online (Sandbox Code Playgroud)

java multithreading interrupted-exception interruption

24
推荐指数
3
解决办法
8万
查看次数

Java - thread.sleep的替代品

我需要暂停一个while循环一段特定的毫秒数.我尝试过使用Thread.sleep(持续时间),但它不准确,特别是在循环方案中.毫秒精度在我的程序中很重要.

这是算法,我不想回去检查条件,直到expectedElapsedTime过去.

while (condition) {
    time = System.currentTimeMillis();
    //do something
    if (elapsedTime(time) < expectedElapsedTime) ) {
        pause the loop  // NEED SUBSTITUTE FOR Thread.sleep()
    }
    // Alternative that I have tried but not giving good results is 
    while ((elapsedTime(time) < expectedElapsedTime)) {
        //do nothing
    }
}

long elapsedTime(long time) {
   long diff = System.currentTimeMillis() - time;
   return diff;
}
Run Code Online (Sandbox Code Playgroud)

java sleep milliseconds

20
推荐指数
3
解决办法
4万
查看次数

要么重新中断此方法,要么重新抛出声纳中的“InterruptedException 问题”

在我的方法之一中,中断异常和执行异常即将到来。我像这样输入了 try catch 。

try{

  //my code
}catch(InterruptedException|ExecutionException e)

  Log.error(" logging it");
  throw new MonitoringException("it failed" , e)


//monitoringexception extends RunTimeException
Run Code Online (Sandbox Code Playgroud)

同样在我的方法中,我抛出了 InterruptedException,ExecutionException

我在声纳中遇到严重错误 - 重新中断此方法或重新抛出“ InterruptedException

有人知道怎么修这个东西吗。

请立即帮助。

java exception try-catch interrupted-exception throws

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

InterruptedException:是什么原因造成的?

关于Java的InterruptedException有一些有趣的问题和答案,例如Java中的InterruptedException处理InterruptedException 的原因.但是,它们都没有告诉我InterruptedException的可能来源.

那些OS信号如SIGTERM,SIGQUIT,SIGINT?在命令行上按CTRL-C会产生InterruptedException吗?还有什么?

java interrupted-exception

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

Java线程休眠和中断异常

  1. 为什么sleep线程需要try catch来捕获Interrupted Exception?
  2. 为什么睡眠会发出中断异常错误?这是我真正想要在java编程中找到的两个问题,我一直在谷歌搜索,我还没有找到一个明确的解释是为什么这两件事发生.

java multithreading sleep exception

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

在等待退出信号时处理InterruptedException(Android中的错误?)

我遇到过下面的代码,我想知道它是否完全符合我的想法:

synchronized(sObject) {
    mShouldExit = true;   
    sObject.notifyAll()    
    while (!mExited) {
      try {
           sObject.wait();
        } catch (InterruptedException ex) {
           Thread.currentThread().interrupt();
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

关于上下文:还有另一个线程检查mShouldExit(在sObject监视器内)并在这种情况下退出.

这看起来对我来说不是一个正确的模式.如果发生中断,它将再次设置中断状态,所以当它返回时sObject.wait(),会出现另一个InterruptedException等等.因此,它永远不会进入真正的等待状态(sObject.wait()),即它永远不会释放sObject监视器.这可能导致无限循环,因为另一个线程无法将mExiting设置为true,因为它永远不会进入sObject的监视器.(所以我认为这个interrupt()电话是错误的,不能在这里使用.)我错过了什么吗?

请注意,代码段是官方Android框架源代码的一部分.

更新:实际上,情况更糟,因为在GL渲染开始时Android中使用了相同的模式.官方源代码GLSurfaceView.GLThread.surfaceCreated():

   public void surfaceCreated() {
        synchronized(sGLThreadManager) {
            if (LOG_THREADS) {
                Log.i("GLThread", "surfaceCreated tid=" + getId());
            }
            mHasSurface = true;
            sGLThreadManager.notifyAll();
            while((mWaitingForSurface) && (!mExited)) {
                try {
                    sGLThreadManager.wait();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

您可以用类似的方式重现错误:确保您的UI线程还有其中断状态标志,然后添加您的GLSurfaceView并启动GL渲染(通过setRenderer(...),但在某些设备上,确保您的GLSurfaceView具有 …

java multithreading android interrupted-exception

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

wait/notify和wait/interrupt有什么区别?

synchronized (Foo.class) {
    while (someCondition) {
        try {
            Foo.class.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎这个线程在其他一些线程调用时interrupt()notify()在此线程上都会唤醒.这两者有什么不同吗?

- 编辑 -

我知道一个用于通知一个对象,另一个用于中断一个线程.但是这两者都导致了相同的结果,也就是说,这个线程被唤醒了,所以我想问的是这两种情况的后果是如何相互不同的.

java multithreading notify wait interruption

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

在线程的情况下Thread.sleep和wait()方法的异常处理

我试图编写一个Producer Consumer模型(java中的Producer线程和Consumer Thread)

我想知道如何处理方法和类的方法InterruptedException引发的Thread.sleepObjectwait()

package producerconsumer;

import java.util.ArrayList;

public class Consumer implements Runnable {

    ArrayList<Integer> container;

    @Override
    public void run() {
        while(true){
        System.out.println("Consumer Thread Running");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(null==container){
            System.out.println("container is null");
        }
        //Removing values from Arraylist
        synchronized (container) {
            if(container.size()==0){
                try {
                    container.wait();
                } catch (InterruptedException e) {
                    //How to tackle this exception
                    e.printStackTrace();
                }
            }
            Integer removedInteger = container.remove(container.size()-1);
            System.out.println("removedInteger..."+removedInteger);
        }
        }
    }

    public void setContainer(ArrayList<Integer> …
Run Code Online (Sandbox Code Playgroud)

java multithreading exception-handling

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

如何处理 BlockingQueue 的 InterruptedException?

我有一个与此类似的代码,它位于run()aRunnable和多个实例的内部方法中Runnable

do{
        try{
            String contractNum=contractNums.take();
           }catch(InterruptedException e){
            logger.error(e.getMessage(), e);
        }
  }while(!("*".equals(contractNum)));
Run Code Online (Sandbox Code Playgroud)

哪里contractNumsBlockingQueue<String>多线程共享的。Runnables这个队列有单独的放置元素。

我不确定捕获后的后续步骤InterruptedException,我应该通过重新抛出 a 来终止该线程RuntimeException(因此我的while循环终止)还是尝试contractNum queue再次获取下一个元素并忽略InterruptedException

我不确定是否InterruptedException被视为线程终止或将其保留在 while 循环中的致命条件。

请建议。

java concurrency multithreading blockingqueue

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

处理 InterruptedException 的最佳实践

在业务场景中,InterruptException 会发生多次,有的在业务代码执行之前,有的在业务代码执行之后。如何处理 InterruptException 让我很困惑。

     1.业务前代码 semaphore.acquire()

        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
           // do something
        }
        resObj = caller.stepTry();
        semaphore.release();
Run Code Online (Sandbox Code Playgroud)
  1. 邮政编码 latch.await()service.take().get()

    CompletionService<CallableResultBO> service = new ExecutorCompletionService<>(executor);
    CountDownLatch latch = new CountDownLatch(size);
    
    for (R callable : listCall){
        callable.setCountParam(JdkThreadCountBO.buildByLatch(latch));
        service.submit(callable);
    }
    
    try {
        latch.await();
    } catch (InterruptedException e) {
        // do something
    }
    
    CallableResultBO[] resArr = new CallableResultBO[size];
    for ( int i = 0; i < size; i++ …
    Run Code Online (Sandbox Code Playgroud)

java concurrency exception

5
推荐指数
2
解决办法
4154
查看次数