How the flow of code in while loop going in android? - android

I am new in Android (Java) coding . I have a small game program downloaded. In that to terminate the thread , have the code
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.setRunning(false);
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
retry = false;
}
}
Here the code author says if catch the exception , the code will go to try part not to the statement retry = false;
I could not believe that. please advise the flow is correct.
Please advise
Thanks
Anes

The loop will be run once. No matter if an Exception is thrown since retry will always be set to false after the first run. If you wanted the loop to continue until no Exception is thrown, you could place the retry = false; directly after the thread.join(); in the try block so it's only being called when you don't get to the catch block.

Related

Tasks.await() is not returning when expected

I am trying to save data into a Firebase RealtimeDatabase.
The process of saving is fine and works just well, but I was curious about an idea I had: To force the .setValue operation into a synchronous structure. I know, that that way isn't the best one, but I wanted to try it regardless of that fact.
So I came up with this code:
Thread t1 = new Thread(){
public void run(){
try {
Tasks.await(databaseReference.child("someChild").setValue(someObject));
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished");
}
In theory, that code snippet is intended to first set the value in the Database, then wait for the completion of that (through the Tasks.await()), and then print out **after that* the line "finished".
I wrapped the Tasks.await()-command in a second thread because when I don't, I get an exception which says something like "Tasks.await() must not be called in the main application thread".
But when I run the whole thing, it just gets stuck at t1.join();.
When I delete the t1.join();-statement, everything works just fine, but the second Thread (t1) isn't finished before the "finished"-output is there. I know why this is like that, but I am nontheless interested in a solution to that problem.

execute sleep() onTouchEvent or Control frame rate in surfaceview (android )

Please check this link. Following code is from this link. I am not sure why it says we have to use thread sleep to limit number of touch events.
I have a surfaceview and gamethread which processes events pushed by touch event. Is this sleep required or should I handle the sleep in gamethread instead by controlling framerate.
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
}
whole code
#Override
public boolean onTouchEvent(MotionEvent event) {
// we only care about down actions in this game.
try {
// history first
int hist = event.getHistorySize();
if (hist > 0) {
// add from oldest to newest
for (int i = 0; i < hist; i++) {
InputObject input = inputObjectPool.take();
input.useEventHistory(event, i);
gameThread.feedInput(input);
}
}
// current last
InputObject input = inputObjectPool.take();
input.useEvent(event);
gameThread.feedInput(input);
} catch (InterruptedException e) {
}
// don't allow more than 60 motion events per second
try {
Thread.sleep(16);
} catch (InterruptedException e) {
}
return true;
}
Your link points to an article that has been posted in 2009, you should really try to find one that is more up to date.
The code itself looks wrong, the author explains that his code is designed to avoid blocking the UI thread but then calls Thread.sleep(16) in onTouchEvent() which is called from the UI thread and blocks it.
What about this example? I haven't read it in detail, but the code looks much better : https://www.codeproject.com/Articles/827608/Android-Basic-Game-Loop

Can I use uncaughtexceptionhandler to move to next block of codes?

Can I use uncaughtexceptionhandler to ignore the exception and move forward?
If I can, how to write that handler?
for example:
try{
//something
}catch(exception e){
//something
}
//AND WHEN SOME UNCAUGHT EXCEPTION APPEAR, IGNORE EXCEPTION AND MOVE TO NEXT STEP
try{
//something else
}catch(exception e){
//something else
}
thank you for your attention
You can use the try .. (catch) .. finally construct to execute code even if an exception was thrown but not handled.
try {
// do things that can throw an exception
} finally {
// this block is !always! executed. Does not matter whether you
// "return"ed, an exception was thrown or everything was just fine.
}
if you add a catch in there then execution order is try up to the point where the exception happened, then the first appropriate catch block, then the finally block.
If you want to execute code in the finally block only if there was no exception, set some flag as the last operation of the try block.
boolean doFinally = true;
try {
// flag stays "true" if something throws here
doFinally = false;
} finally {
if (doFinally) {
// do something
}
}
Note: if you don't catch an otherwise uncaught exception somewhere your app will still crash after it has exectued the code in finally.

Calling Drive API for Android on Separate thread returns no data

I am using the Drive API v2 for android, and when I execute the following method my app seems to pause or wait, and no data is returned.
public About getAbout() throws InterruptedException, ExecutionException {
FutureTask<About> future = new FutureTask<About>(new Callable<About>() {
public About call() throws IOException {
About about = null;
try {
about = _driveService.about().get().execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return about;
}
});
About about = future.get();
return about;
}
Does anyone have any idea what I'm doing wrong?
You are creating a FutureTask, but you are never executing it (not on the current thread and not on any other). Then you call future.get() which will block until the operation is completed. Since you never actually perform the operation, it will wait forever.
To execute an operation on a background thread you could for example use http://developer.android.com/reference/android/os/AsyncTask.html

Android Gameloop Thread.join() hangs application

Hey all i am implementing the gameloop found here: http://obviam.net/index.php/the-android-game-loop/
My question is why does having:
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
In my surfaceDestroyed() function of my game view hang the application?
thread.join() will BLOCK until the thread you're joining completes. If that thread never completes, that function will never release.
Because thread.join() blocks the calling thread until thread is done.

Categories

Resources