简体   繁体   中英

Does inner thread dies when parent function terminates

My question was not clear and poorly written, sorry about that. I'll try to give it another shot.

So, I have a queue that is filled by another program until it gets a stop signal. I don't know when it stops so what I want to do is in a thread process all the elements of the queue until it is empty and print some information about its elements. And I want to see if it finished processing in another class. To be able to check if it finished its operations I have a singleton class that maps Future's with some string information. Here is code;

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");
} 

So, in another class I take my Future from the map and wait until it is done;

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);
        }
    }
} 

Here I see the Future is not finished its job and it runs for like 2-3 secs more, but my problem is eventhough Future is active, in my log I cannot see anything between "Thread still alive" logs. What I expect is since the Future is not done yet, it should be processing some more queue elements and printing information about them. I suspect there is a mistake because I see that queue filler program sent more elements to the queue than I found in my log. Am I missing something here?

No, It won't die. You can use Callable if you want to wait till your thread finishes execution.

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();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM