yanny
05-29-2012, 09:47 AM
Hi, I have this code for updating SurfaceView when update occurred. But something I do wrong, because sometimes it scrash. I thing that there are somewhere deadlocks. Here is code:
public class CalcView extends SurfaceView implements Runnable {
public Display display;
private Thread thread = null;
private SurfaceHolder holder;
private boolean runOK = false;
public CalcView(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
display = new Display(context);
display.setOnChangeListener(new Display.onChangeListener() {
public void onChange() {
resumeThread();
}
});
setOnTouchListener(display);
setFocusable(true);
requestFocus();
setFocusableInTouchMode(true);
}
@Override
protected void onDraw(Canvas canvas) {
display.Draw(canvas);
}
@Override
public void run() {
while (runOK) {
if (!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
onDraw(canvas);
holder.unlockCanvasAndPost(canvas);
synchronized (thread) {
if (thread.getState() != Thread.State.WAITING) {
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void resumeThread() {
try {
if ((thread == null) || !holder.getSurface().isValid())
return;
synchronized (thread) {
while (thread.getState() == Thread.State.RUNNABLE)
;
thread.notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void pause() {
try {
runOK = false;
while (true) {
try {
resumeThread();
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void resume() {
try {
runOK = true;
if (thread != null)
while (thread.getState() == Thread.State.RUNNABLE)
;
thread = new Thread(this);
synchronized (thread) {
try {
thread.start();
} catch (IllegalThreadStateException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
display.onSizeChanged(w, h);
}
}
If you know better way for doing this, please write me a message.
public class CalcView extends SurfaceView implements Runnable {
public Display display;
private Thread thread = null;
private SurfaceHolder holder;
private boolean runOK = false;
public CalcView(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
display = new Display(context);
display.setOnChangeListener(new Display.onChangeListener() {
public void onChange() {
resumeThread();
}
});
setOnTouchListener(display);
setFocusable(true);
requestFocus();
setFocusableInTouchMode(true);
}
@Override
protected void onDraw(Canvas canvas) {
display.Draw(canvas);
}
@Override
public void run() {
while (runOK) {
if (!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
onDraw(canvas);
holder.unlockCanvasAndPost(canvas);
synchronized (thread) {
if (thread.getState() != Thread.State.WAITING) {
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void resumeThread() {
try {
if ((thread == null) || !holder.getSurface().isValid())
return;
synchronized (thread) {
while (thread.getState() == Thread.State.RUNNABLE)
;
thread.notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void pause() {
try {
runOK = false;
while (true) {
try {
resumeThread();
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void resume() {
try {
runOK = true;
if (thread != null)
while (thread.getState() == Thread.State.RUNNABLE)
;
thread = new Thread(this);
synchronized (thread) {
try {
thread.start();
} catch (IllegalThreadStateException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
display.onSizeChanged(w, h);
}
}
If you know better way for doing this, please write me a message.