我有扩展 SurfaceView 的 GraphicView。我需要它来绘制图形。我还需要 onTouchEvent。但问题是......我不知道如何描述它:)这是我的代码:
public class GraphicView extends SurfaceView implements Runnable {
private boolean mutexOpened = true;
public boolean onTouchEvent( MotionEvent me ) {
if ( mutexOpened ) {
mutexOpened = false;
Log.d( "mutex", "ACTION 1" );
switch ( action ) {
case MotionEvent.ACTION_DOWN: {
int rnd = new Random().nextInt( 40000 ) + 1000;
for ( int i = 0; i < rnd; i++ ) {} // it's some long action :)
Log.d( "mutex", "ACTION 2: down" );
break;
}
}
Log.d( "mutex", "ACTION 2: end" );
mutexOpened = true;
}
}
public void run() {
while ( true ) {
if ( mutexOpened ) {
Log.d( "mutex", "!!! RUN !!!!" );
}
}
}
}
我使用(我想)必须控制我的线程的互斥技术。但在日志中我看到以下内容:
!!! RUN !!!!
ACTION 1
!!! RUN !!!!
ACTION 2: down
ACTION 2: end
但为什么??为什么是第二个“!!! RUN !!!!” 互斥锁关闭时在“ACTION 1”和“ACTION 2”之间运行?不可能!:)))
我试着下一步做:
public void run() {
while ( true ) {
if ( mutexOpened ) {
mutexOpened = false; // close mutex
Log.d( "mutex", "!!! RUN !!!!" );
mutexOpened = true; // open mutex
}
}
}
但是... FAIL :)) onTouchEvent 根本不会运行 :D))) 有人知道如何解决这个问题吗?