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

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘