I am new to multithreading with Java. I did some research, read the tutorials and did the tests, but I'm stuck with this problem. Basically, I create a game’s skeleton, and I would like to have a main activity class, a stream class that contains methods, performs slow operations (reading files and unpacking contents into buffers) and has a stream, which is a game loop that responds to user interface operations .
Firstly, I have a main activity class that creates and starts a separate thread:
public class ExperimentsActivity extends Activity {
Then I have a MainLoopThread class that implements a thread for game logic:
public class MainLoopThread implements Runnable { public boolean running; private TestClass baseData;
And finally, the GFXUnpack thread GFXUnpack , which contains the code that opens the file on the SD card, reads the material in it and writes it to buffers:
public class GFXUnpack implements Runnable {
When I run the above, the debugger output is:
MainLoopThread Opening the file: waiting ...
GFXUnpack Mapping File
GFXUnpack Mapped File
GFXUnpack File Open Notification
Then the application freezes and I need to force close it. I thought, as I call notifyAll() in the run() GFXUnpack (in the finally{} block), that the caller's thread (MainLoopThread) will continue, and I will see the debugger message “Opening file completed”, but the application hangs instead.
Does anyone have any idea why this is happening?