Race condition in OFAndroidVideoPlayer?

I don’t really know Java or multithreading in general, but I’ve been experiencing this problem for the past six months where sometimes movies on Android would never finish… Specifically, getIsMovieDone() would never return true.

I think it might be because the completion callback is coming in on one thread, and sometimes the update loop on the main thread is overwriting it. Look how isMovieDone is set to true by thread 29218 and then set to false by thread 29256 in between my two calls to isMovieDone():

If I change OFAndroidVideoPlayer.update() to only do its business if the movie wasn’t done (bIsMoviedone == false) I don’t get anymore hangs:

public boolean update() {
		synchronized(this){
			if(bIsFrameNew && bIsMoviedone == false) {
				if(surfaceTexture != null) surfaceTexture.updateTexImage();
				bIsFrameNew = false;
				bIsMoviedone = false;
				return true;
			} else {
				return false;
			}
		}
	}

Buuut the nature of race conditions is probabilistic, so I’m not sure if this change actually fixes things. I also have no idea about this synchronized keyword. Maybe instead of this additional if-check, I should just make the completion callback synchronized?

Or maybe it would be simpler if OFAndroidVideoPlayer.update() just didn’t set bIsMovieDone. Why must that be the case?

i think the way to solve it is for the thread to set a flag that update will check and then set bisFrameNew to whatever that flag is. once the video starts again update needs to set the flag to false