[android-developers] Re: Menu option starts Thread - menu hangs.
Could you provide a code-snippet showing how you coded the menu- handling and the starting of your thread? On Mar 12, 10:00 am, Ikon wrote: > Hi, > > I am developing a game based on the SurfaceView/LunarLander example in > the sample code. > I have one activity, with one menu option that is "new game". > > My activity listens for the menu selection, and once new game is > selected, doStart of the Game thread is called. The game starts up > after 3-4 seconds, but the menu option sticks around on the screen > until the game has started. I think the main thread is waiting on the > doStart to do its thing, but since it's creating a new thread, how do > I get it to just start the thread and continue its business of hiding > the menu option right away, instead of after 3 seconds? > > Thanks, > Ayan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Menu option starts Thread - menu hangs.
Streets, Here is the snippet where I do the menuHandling: @Override public boolean onOptionsItemSelected(MenuItem item) { switch( item.getItemId()){ case START_GAME: gameThread.doStart(); return true; case STOP_GAME: gameThread.setState(gameThread.STATE_LOSE); return true; case PAUSE_GAME: gameThread.pause(); return true; case RESUME_GAME: gameThread.unpause(); return true; } return super.onOptionsItemSelected(item); } Here is my doStart method: public void doStart() { synchronized (mSurfaceHolder) { createShapes(mContext); shape[0].visible = true; score = 0; mLastTime = System.currentTimeMillis() + 100; setState(STATE_RUNNING); } } Thanks, Ayan On Mar 12, 10:28 am, Streets Of Boston wrote: > Could you provide a code-snippet showing how you coded themenu- > handling and the starting of yourthread? > > On Mar 12, 10:00 am, Ikon wrote: > > > Hi, > > > I am developing a game based on the SurfaceView/LunarLander example in > > the sample code. > > I have one activity, with onemenuoption that is "new game". > > > My activity listens for themenuselection, and once new game is > > selected, doStart of the Gamethreadis called. The game starts up > > after 3-4 seconds, but themenuoption sticks around on the screen > > until the game has started. I think the mainthreadis waiting on the > > doStart to do its thing, but since it's creating a newthread, how do > > I get it to just start thethreadand continue its business of hiding > > themenuoption right away, instead of after 3 seconds? > > > Thanks, > > Ayan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Menu option starts Thread - menu hangs.
I'm assuming that gameThread is something that has its own thread and runs the game. And the main thread (on which onOptionsItemSelected is running) is seperate and handles the user-interface. If so, the it may be that createShapes() is pretty expensive and takes a while. This'll hang the menu-option item until it's done and ready. You could startup the game thread in yet another thread. This other thread could be your gameThread. But i don't know its interface and whether it has something that does except jobs/tasks, e.g. gameThread.submitTask(new Runnable() { public void run() { dosomething } }); Let's assume that it doesn't and this submitTask on gameThread does not exist. Then you have to create another thread to do this. I would use the java.util.concurrent package for this: in your activity you declare an instance variable: private ExecutorService mGameStarter; in your onCreate of your activity: mGameStarter = Executors.newSingleThreadExecutor(); in your onDestroy: mGameStarter.shutdownNow(); And in your onOptionsItemSelected, inside your case START_GAME mGameStarter.submit(new Runnable() { public void run() { gameThread.doStart(); } }); Every time you call submit() the new thread (hed by mGameStarter) will wake up and execute the Runnable you specified when calling submit(). When the Runnable ends, this thread will sleep until you submit yet another task. On Mar 12, 9:48 pm, Ikon wrote: > Streets, > > Here is the snippet where I do the menuHandling: > > @Override > public boolean onOptionsItemSelected(MenuItem item) { > > switch( item.getItemId()){ > case START_GAME: > gameThread.doStart(); > return true; > case STOP_GAME: > gameThread.setState(gameThread.STATE_LOSE); > return true; > case PAUSE_GAME: > gameThread.pause(); > return true; > case RESUME_GAME: > gameThread.unpause(); > return true; > } > > return super.onOptionsItemSelected(item); > } > > Here is my doStart method: > > public void doStart() { > synchronized (mSurfaceHolder) { > > createShapes(mContext); > shape[0].visible = true; > score = 0; > > mLastTime = System.currentTimeMillis() + 100; > setState(STATE_RUNNING); > } > } > > Thanks, > Ayan > > On Mar 12, 10:28 am, Streets Of Boston > wrote: > > > > > Could you provide a code-snippet showing how you coded themenu- > > handling and the starting of yourthread? > > > On Mar 12, 10:00 am, Ikon wrote: > > > > Hi, > > > > I am developing a game based on the SurfaceView/LunarLander example in > > > the sample code. > > > I have one activity, with onemenuoption that is "new game". > > > > My activity listens for themenuselection, and once new game is > > > selected, doStart of the Gamethreadis called. The game starts up > > > after 3-4 seconds, but themenuoption sticks around on the screen > > > until the game has started. I think the mainthreadis waiting on the > > > doStart to do its thing, but since it's creating a newthread, how do > > > I get it to just start thethreadand continue its business of hiding > > > themenuoption right away, instead of after 3 seconds? > > > > Thanks, > > > Ayan- Hide quoted text - > > - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Menu option starts Thread - menu hangs.
Thanks for the comprehensive response - that is very helpful! What I dont understand is that if I am just telling another thread to go do processing, why would THAT thread not block the main thread then? On Mar 12, 10:52 pm, Streets Of Boston wrote: > I'm assuming that gameThread is something that has its ownthreadand > runs the game. And the mainthread(on which onOptionsItemSelected is > running) is seperate and handles the user-interface. > > If so, the it may be that createShapes() is pretty expensive and takes > a while. This'll hang themenu-option item until it's done and ready. > > You could startup the gamethreadin yet anotherthread. > This otherthreadcould be your gameThread. But i don't know its > interface and whether it has something that does except jobs/tasks, > e.g. gameThread.submitTask(new Runnable() { public void run() > { dosomething } }); Let's assume that it doesn't and this submitTask > on gameThread does not exist. > > Then you have to create anotherthreadto do this. I would use the > java.util.concurrent package for this: > > in your activity you declare an instance variable: > private ExecutorService mGameStarter; > > in your onCreate of your activity: > mGameStarter = Executors.newSingleThreadExecutor(); > > in your onDestroy: > mGameStarter.shutdownNow(); > > And in your onOptionsItemSelected, inside your case START_GAME > mGameStarter.submit(new Runnable() { > public void run() { > gameThread.doStart(); > } > }); > > Every time you call submit() the newthread(hed by mGameStarter) will > wake up and execute the Runnable you specified when calling submit(). > When the Runnable ends, thisthreadwill sleep until you submit yet > another task. > > On Mar 12, 9:48 pm, Ikon wrote: > > > Streets, > > > Here is the snippet where I do the menuHandling: > > > @Override > > public boolean onOptionsItemSelected(MenuItem item) { > > > switch( item.getItemId()){ > > case START_GAME: > > gameThread.doStart(); > > return true; > > case STOP_GAME: > > gameThread.setState(gameThread.STATE_LOSE); > > return true; > > case PAUSE_GAME: > > gameThread.pause(); > > return true; > > case RESUME_GAME: > > gameThread.unpause(); > > return true; > > } > > > return super.onOptionsItemSelected(item); > > } > > > Here is my doStart method: > > > public void doStart() { > > synchronized (mSurfaceHolder) { > > > createShapes(mContext); > > shape[0].visible = true; > > score = 0; > > > mLastTime = System.currentTimeMillis() + 100; > > setState(STATE_RUNNING); > > } > > } > > > Thanks, > > Ayan > > > On Mar 12, 10:28 am, Streets Of Boston > > wrote: > > > > Could you provide a code-snippet showing how you coded themenu- > > > handling and the starting of yourthread? > > > > On Mar 12, 10:00 am, Ikon wrote: > > > > > Hi, > > > > > I am developing a game based on the SurfaceView/LunarLander example in > > > > the sample code. > > > > I have one activity, with onemenuoption that is "new game". > > > > > My activity listens for themenuselection, and once new game is > > > > selected, doStart of the Gamethreadis called. The game starts up > > > > after 3-4 seconds, but themenuoption sticks around on the screen > > > > until the game has started. I think the mainthreadis waiting on the > > > > doStart to do its thing, but since it's creating a newthread, how do > > > > I get it to just start thethreadand continue its business of hiding > > > > themenuoption right away, instead of after 3 seconds? > > > > > Thanks, > > > > Ayan- Hide quoted text - > > > - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---
[android-developers] Re: Menu option starts Thread - menu hangs.
I guess the reason is that ExecutorService.submit must be doing something different than just starting up a new thread? On Mar 12, 10:52 pm, Streets Of Boston wrote: > I'm assuming that gameThread is something that has its ownthreadand > runs the game. And the mainthread(on which onOptionsItemSelected is > running) is seperate and handles the user-interface. > > If so, the it may be that createShapes() is pretty expensive and takes > a while. This'll hang themenu-option item until it's done and ready. > > You could startup the gamethreadin yet anotherthread. > This otherthreadcould be your gameThread. But i don't know its > interface and whether it has something that does except jobs/tasks, > e.g. gameThread.submitTask(new Runnable() { public void run() > { dosomething } }); Let's assume that it doesn't and this submitTask > on gameThread does not exist. > > Then you have to create anotherthreadto do this. I would use the > java.util.concurrent package for this: > > in your activity you declare an instance variable: > private ExecutorService mGameStarter; > > in your onCreate of your activity: > mGameStarter = Executors.newSingleThreadExecutor(); > > in your onDestroy: > mGameStarter.shutdownNow(); > > And in your onOptionsItemSelected, inside your case START_GAME > mGameStarter.submit(new Runnable() { > public void run() { > gameThread.doStart(); > } > }); > > Every time you call submit() the newthread(hed by mGameStarter) will > wake up and execute the Runnable you specified when calling submit(). > When the Runnable ends, thisthreadwill sleep until you submit yet > another task. > > On Mar 12, 9:48 pm, Ikon wrote: > > > Streets, > > > Here is the snippet where I do the menuHandling: > > > @Override > > public boolean onOptionsItemSelected(MenuItem item) { > > > switch( item.getItemId()){ > > case START_GAME: > > gameThread.doStart(); > > return true; > > case STOP_GAME: > > gameThread.setState(gameThread.STATE_LOSE); > > return true; > > case PAUSE_GAME: > > gameThread.pause(); > > return true; > > case RESUME_GAME: > > gameThread.unpause(); > > return true; > > } > > > return super.onOptionsItemSelected(item); > > } > > > Here is my doStart method: > > > public void doStart() { > > synchronized (mSurfaceHolder) { > > > createShapes(mContext); > > shape[0].visible = true; > > score = 0; > > > mLastTime = System.currentTimeMillis() + 100; > > setState(STATE_RUNNING); > > } > > } > > > Thanks, > > Ayan > > > On Mar 12, 10:28 am, Streets Of Boston > > wrote: > > > > Could you provide a code-snippet showing how you coded themenu- > > > handling and the starting of yourthread? > > > > On Mar 12, 10:00 am, Ikon wrote: > > > > > Hi, > > > > > I am developing a game based on the SurfaceView/LunarLander example in > > > > the sample code. > > > > I have one activity, with onemenuoption that is "new game". > > > > > My activity listens for themenuselection, and once new game is > > > > selected, doStart of the Gamethreadis called. The game starts up > > > > after 3-4 seconds, but themenuoption sticks around on the screen > > > > until the game has started. I think the mainthreadis waiting on the > > > > doStart to do its thing, but since it's creating a newthread, how do > > > > I get it to just start thethreadand continue its business of hiding > > > > themenuoption right away, instead of after 3 seconds? > > > > > Thanks, > > > > Ayan- Hide quoted text - > > > - Show quoted text - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~--~~~~--~~--~--~---