Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Should use a semaphore instead of locks. Subtle bug in that there is …
Browse files Browse the repository at this point in the history
…no guarantee that wait() will be called before notify().
  • Loading branch information
Xyresic committed Dec 6, 2016
1 parent c947584 commit 33175da
Showing 1 changed file with 6 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Semaphore;

public class TessBaseAPITest extends TestCase {
@SuppressLint("SdCardPath")
Expand Down Expand Up @@ -725,17 +726,15 @@ public void testStop() throws InterruptedException {
}
final Bitmap bmp = getTextImage(inputTextBuilder.toString(), 640, 4000);

final Object progressLock = new Object();
final Semaphore progressSem = new Semaphore(0);
final TessBaseAPI baseApi = new TessBaseAPI(new ProgressNotifier() {
@Override
public void onProgressValues(ProgressValues progressValues) {
if (progressValues.getPercent() > 50){
fail("OCR recognition was too fast, try to increase the image size and amount of text?");
}
if (progressValues.getPercent() > 1){
synchronized (progressLock){
progressLock.notify();
}
progressSem.release();
}
}
});
Expand All @@ -744,9 +743,7 @@ class LongRecognitionTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
baseApi.getHOCRText(0);
synchronized (progressLock){
progressLock.notify();
}
progressSem.release();
return null;
}
}
Expand All @@ -759,17 +756,13 @@ protected Void doInBackground(Void... params) {
task.execute();

// Wait for recognition to start
synchronized (progressLock){
progressLock.wait();
}
progressSem.acquire();

baseApi.stop();

// Wait for getHOCRText() to complete, otherwise we may end() and recycle baseApi before
// getHOCRText() finishes execution on the AsyncTask thread and cause an exception
synchronized (progressLock){
progressLock.wait();
}
progressSem.acquire();

baseApi.end();
bmp.recycle();
Expand Down

0 comments on commit 33175da

Please sign in to comment.