mirror of
https://github.com/zxing/zxing.git
synced 2025-02-21 02:55:27 -08:00
Replaced busy wait with wait()/notifyAll() idiom
git-svn-id: https://zxing.googlecode.com/svn/trunk@206 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
4c4adc953e
commit
848f791d4f
|
@ -32,9 +32,11 @@ import com.google.zxing.Result;
|
||||||
*/
|
*/
|
||||||
final class WorkerThread extends Thread {
|
final class WorkerThread extends Thread {
|
||||||
|
|
||||||
private CameraSurfaceView surfaceView;
|
private final CameraSurfaceView surfaceView;
|
||||||
private CameraManager cameraManager;
|
private final CameraManager cameraManager;
|
||||||
private Handler handler;
|
private final Handler handler;
|
||||||
|
private final Object idleLock;
|
||||||
|
private State state;
|
||||||
|
|
||||||
private enum State {
|
private enum State {
|
||||||
IDLE,
|
IDLE,
|
||||||
|
@ -43,12 +45,11 @@ final class WorkerThread extends Thread {
|
||||||
DONE
|
DONE
|
||||||
}
|
}
|
||||||
|
|
||||||
private State state;
|
|
||||||
|
|
||||||
WorkerThread(CameraSurfaceView surfaceView, CameraManager cameraManager, Handler handler) {
|
WorkerThread(CameraSurfaceView surfaceView, CameraManager cameraManager, Handler handler) {
|
||||||
this.surfaceView = surfaceView;
|
this.surfaceView = surfaceView;
|
||||||
this.cameraManager = cameraManager;
|
this.cameraManager = cameraManager;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
|
this.idleLock = new Object();
|
||||||
state = State.IDLE;
|
state = State.IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,10 +58,7 @@ final class WorkerThread extends Thread {
|
||||||
while (true) {
|
while (true) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case IDLE:
|
case IDLE:
|
||||||
try {
|
idle();
|
||||||
sleep(50);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case PREVIEW_LOOP:
|
case PREVIEW_LOOP:
|
||||||
surfaceView.capturePreviewAndDraw();
|
surfaceView.capturePreviewAndDraw();
|
||||||
|
@ -89,18 +87,37 @@ final class WorkerThread extends Thread {
|
||||||
|
|
||||||
public void requestPreviewLoop() {
|
public void requestPreviewLoop() {
|
||||||
state = State.PREVIEW_LOOP;
|
state = State.PREVIEW_LOOP;
|
||||||
|
wakeFromIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestStillAndDecode() {
|
public void requestStillAndDecode() {
|
||||||
state = State.STILL_AND_DECODE;
|
state = State.STILL_AND_DECODE;
|
||||||
|
wakeFromIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestExitAndWait() {
|
public void requestExitAndWait() {
|
||||||
state = State.DONE;
|
state = State.DONE;
|
||||||
|
wakeFromIdle();
|
||||||
try {
|
try {
|
||||||
join();
|
join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void idle() {
|
||||||
|
try {
|
||||||
|
synchronized (idleLock) {
|
||||||
|
idleLock.wait();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
// continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void wakeFromIdle() {
|
||||||
|
synchronized (idleLock) {
|
||||||
|
idleLock.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue