ThreadpoolExecutor数据混乱

时间:2018-09-07 07:44:23

标签: android threadpoolexecutor flir

我正在使用android的线程池执行程序框架(初始化如下)。

BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
ExecutorService executorService = new ThreadPoolExecutor(totalCores, totalCores * 3, 10, TimeUnit.SECONDS, taskQueue);

现在,考虑以下函数onFrameProcessed-

public void onFrameProcessed(RenderedImage renderedImage) {
String timeNow = new SimpleDateFormat("d-M-Y_HH_mm_ss_SSS").format(new Date()).toString();
CustomRunnable3 customRunnable3 = new CustomRunnable3(renderedImage, timeNow);
executorService.execute(customRunnable3);
}

CustomRunnable3的定义如下:

class CustomRunnable3 implements Runnable {
    RenderedImage renderedImageLocal;
    String basePath, timeNowCopy;
    int hashCode;

    CustomRunnable3(RenderedImage renderedImage, String timeNow) {
        renderedImageLocal = renderedImage;
        this.basePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        this.timeNowCopy = timeNow;
        hashCode = renderedImageLocal.hashCode();
    }

    @Override
    public void run() {
        if (renderedImageLocal.imageType() == RenderedImage.ImageType.ThermalRadiometricKelvinImage) {
            int[] thermalData = renderedImageLocal.thermalPixelValues();
            String dataPath = basePath + "/" + this.timeNowCopy + ".csv";
            try {
                PrintWriter printWriter = new PrintWriter(dataPath);
                int dataLen = thermalData.length;
                for (int i = 0; i < dataLen; i++) {
                    printWriter.println(thermalData[i]);
                }
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String imgPath = basePath + "/" + this.timeNowCopy + ".jpg";
            try {
                if (hashCode != renderedImageLocal.hashCode()) {
                    Log.e("Checking", "Hash code changed..");
                }
                renderedImageLocal.getFrame().save(new File(imgPath), frameProcessor);
                if (hashCode != renderedImageLocal.hashCode()) {
                    Log.e("Checking", "Hash code changed after writing..");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

使用场景:每秒调用onFrameReceived多次(如4-5次)。在对onFrameReceived的每次调用中,我都保存了renderedImage对象的两个文件(1个csv文件,1个jpg文件)。这两个文件必须相互关联,因为它们都是从一个父级创建的,并且具有相同的名称(扩展名除外)。

问题:但这并没有发生,并且我最终以某种方式来处理来自1个renderedImage的jpg文件内容和来自另一个renderedImage对象的csv内容。 造成此问题的可能原因是什么,请分享您的看法。

0 个答案:

没有答案