prim_sun 2020-03-19 11:59 采纳率: 0%
浏览 1263

Java多线程ThreadPoolExecutor类的shutdown方法使用后,线程的任务没有执行完就terminate

我使用了ThreadPoolExecutor.execute方法执行了两个MyThread类的对象,之后调用ThreadPoolExecutor.shutdown方法。这个方法不是等待提交的任务执行完才终止吗,为什么我提交的两个任务都没有被执行呢?

String adbHead = "adb ";
                            try {
                                if (EasyScreenshotAction.DevicePickerDialog.serial ==null || EasyScreenshotAction.DevicePickerDialog.serial.length()<1) {
                                    //serial是序列号
                                }else {
                                    adbHead="adb -s "+EasyScreenshotAction.DevicePickerDialog.serial+" ";
                                }
                                String  mobileScreenshot=getCurrentTime()+"_screenshot.png";
                                String  mobileXml=getCurrentTime()+"_xml.uix";
                                //使用多线程,来达到更快的同步速度
                                MyThread screenThread=new MyThread(adbHead+"shell /system/bin/screencap -p /data/local/tmp/"+mobileScreenshot);
                                //System.out.println(adbHead+"shell /system/bin/screencap -p /data/local/tmp/"+mobileScreenshot);
                                MyThread xmlThread=new MyThread(adbHead+"shell uiautomator dump /data/local/tmp/"+mobileXml);
                                //System.out.println(adbHead+"shell uiautomator dump /data/local/tmp/"+mobileXml);
                                monitor.setTaskName("Obtaining device screenshot");
                                monitor.subTask("Taking UI XML snapshot...");
                                ThreadPoolExecutor executor = new ThreadPoolExecutor(5,5, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());                           

//                              Runtime.getRuntime().exec(adbHead+"shell /system/bin/screencap -p /data/local/tmp/"+mobileScreenshot).waitFor();
//                              System.out.println("截图执行完毕");
                                executor.execute(xmlThread);

//                              Runtime.getRuntime().exec(adbHead+"shell uiautomator dump /data/local/tmp/"+mobileXml).waitFor();

                                executor.execute(screenThread);


                                executor.shutdown();//启动关闭,执行原来提交的任务且不接受新的任务

请问我哪里出问题了呢?

  • 写回答

1条回答 默认 最新

  • 人到中年就秃头 2020-03-19 12:51
    关注

    查看代码是否在run方法中,如过你那个Runtime.getRuntime().exec()必须运行,建议写在run方法里面

    import java.io.IOException;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    /**
     * Test
     *
     * @author admin
     * @createTime 2020/3/19
     */
    public class Test {
        public static void main(String[] args) throws IOException, InterruptedException {
            //使用多线程,来达到更快的同步速度
            MyThread screenThread = new MyThread("shell /system/bin/screencap -p /data/local/tmp/");
            MyThread xmlThread = new MyThread("shell uiautomator dump /data/local/tmp/");
    
            Runtime.getRuntime().exec(screenThread.value).waitFor();
            Runtime.getRuntime().exec(xmlThread.value).waitFor();
    
            ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
            executor.execute(xmlThread);
            executor.execute(screenThread);
            executor.shutdown();//启动关闭,执行原来提交的任务且不接受新的任务
        }
    }
    
    class MyThread implements Runnable {
        public MyThread(String value) {
            this.value = value;
        }
    
        public String value;
    
        @Override
        public void run() {
            try {
                Runtime.getRuntime().exec(value).waitFor();
                System.out.println("你启动了线程:" + value);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥30 C++行情软件的tick数据如何高效的合成K线