有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当父函数终止时,java不会导致内线程死亡

我的问题不清楚,写得不好,很抱歉。我会试着再试一次

所以,我有一个由另一个程序填充的队列,直到它收到停止信号。我不知道什么时候停止,所以我想做的是 在线程中,处理队列的所有元素,直到队列为空,并打印有关其元素的一些信息。 我想看看它是否在另一个类中完成了处理。能够检查它是否完成了操作 我有一个单例类,它用一些字符串信息映射未来的。这是代码

public void complete(){
    ExecutorService executor = Executors.newSingleThreadExecutor();
        class specCallable implements Callable<Boolean>{
            boolean isOK = true;

            @Override
            public Boolean call() throws Exception {
                //while queue is not empty get its elements and print some information about them
            }
            return isOK;
        }
        specCallable specRun = new specCallable(); 
        Future<Boolean> future = executor.submit(specRun);
        //I will put that future in my map to get it in another class and check if it is done 
        FutureMap futureMap = FutureMap.getInstance();
        futureMap.addFuture(future, "FFT");
} 

所以,在另一节课上,我从地图上取下我的未来,等待它完成

public void finishOperation() throws InterruptedException, ExecutionException{
    FutureMap futureMap = FutureMap.getInstance();
    Future f = futureMap.getFuture("FFT");
    if(f!=null){
        while(!f.isDone()){
            System.out.println("Thread still alive");
            Thread.sleep(100);
        }
    }
} 

在这里,我看到未来还没有完成它的工作,它还要运行2-3秒,但我的问题是,即使未来是活跃的, 在我的日志中,我看不到“线程仍处于活动状态”日志之间的任何内容。我所期望的是,因为未来还没有结束, 它应该处理更多的队列元素并打印关于它们的信息。我怀疑这是个错误,因为我看到了 队列填充程序向队列发送的元素比我在日志中找到的多。我是不是遗漏了什么


共 (1) 个答案

  1. # 1 楼答案

    不,它不会死的。如果要等待线程完成执行,可以使用Callable

    public class MyCallable implements Callable<String> {
    
        @Override
        public String call() throws Exception {
            Thread.sleep(1000);
            //return the thread name executing this callable task
            return Thread.currentThread().getName();
        }
        
        public static void main(String args[]){
            //Get ExecutorService from Executors utility class, thread pool size is 10
            ExecutorService executor = Executors.newFixedThreadPool(10);
            //create a list to hold the Future object associated with Callable
            List<Future<String>> list = new ArrayList<Future<String>>();
            //Create MyCallable instance
            Callable<String> callable = new MyCallable();
            for(int i=0; i< 100; i++){
                //submit Callable tasks to be executed by thread pool
                Future<String> future = executor.submit(callable);
                //add Future to the list, we can get return value using Future
                list.add(future);
            }
            for(Future<String> fut : list){
                try {
                    //print the return value of Future, notice the output delay in console
                    // because Future.get() waits for task to get completed
                    System.out.println(new Date()+ "::"+fut.get());
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
            //shut down the executor service now
            executor.shutdown();
        }
    
    }