Why does the following executor service java Thread program doesn't shut down?

前端 未结 1 491
刺人心
刺人心 2021-01-17 01:30
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Input         


        
1条回答
  •  无人及你
    2021-01-17 02:12

    Look at your loop:

    for(int year = 11; year <= 13; year++){
            for(int i = 1; i <= 350; i++){
                //generating 1050 URLs at one shot
                StringBuffer regNo = new StringBuffer("1111").append(year).append("111").append(String.format("%03d", i));
    
    
                String url = "magicUrl" + regNo;
                System.out.println(url);
                worker = new MyRunnable(url, regNo.toString());
    
            }    
    }
    

    You're overwriting worker each time through the loop, so by the time you get to executor.execute(worker);, worker holds the last value you assigned to it, which will be the runnable created from the last url generated.

    Try replacing the line worker = new MyRunnable(url, regNo.toString()); with executor.execute(new MyRunnable(url, regNo.toString())); and see if that fixes it.

    0 讨论(0)
提交回复
热议问题