调用Thread.start()时onSurfaceTextureUpdated中的异常

时间:2017-12-04 00:18:49

标签: java android multithreading ocr tess-two

我正在为android编写基于Tess-two的OCR系统,偶然发现了一个奇怪的障碍。我实际上使用TextureView与已弃用的Camera相关联,当我尝试在华为P8 上执行此代码时:

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    Log.d("frame", "update frame");
    if(btmQueue.size()<10)
        btmQueue.add(mTextureView.getBitmap());
    System.out.println(mOCRWorker.isAlive());
    if(mOCRWorker.isAlive()==false)
    {
        mOCRWorker.start();
    }
}

Thread mOCRWorker = new Thread(new Runnable() {

    @Override
    public void run() {
        Log.d("Ocr:", "Running thread!");
        if(apiInit==true) {
            if (btmQueue.size() > 0) {
                Bitmap bmp = btmQueue.pollFirst();
                baseAPI.setImage(bmp);
                String recognizedText = baseAPI.getUTF8Text();
                Log.d("Recognized: ", recognizedText);
            }
        }
        Log.d("Ocr:", "Thread finished!");

    }
});

我收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.myApp, PID: 18386
java.lang.IllegalThreadStateException: Thread already started
at java.lang.Thread.checkNotStarted(Thread.java:864)
at java.lang.Thread.start(Thread.java:1074) [...]

我认为如果isAlive()给我假,那么我的线程实际上已经死了。你有什么线索吗?

1 个答案:

答案 0 :(得分:0)

哦,我明白了: 对于任何Thread,方法start()都不能被多次调用 - 甚至是特别是在执行Thread时。我把while(ocrShouldWork)循环放在我的Thread中,让它保持工作直到我关闭它。