【发布时间】:2012-11-02 14:12:17
【问题描述】:
我有 GraphicView,它扩展了 SurfaceView。我需要它来绘制图形。我还需要 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))) 有人知道如何解决这个问题吗?
【问题讨论】:
-
@PBrando,我认为您理解正确。它在第二个线程上运行,但我需要同步这些线程... :)
标签: java android mutex touch-event runnable