我使用了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();//启动关闭,执行原来提交的任务且不接受新的任务
请问我哪里出问题了呢?
查看代码是否在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();
}
}
}