如果我使用try catch并最终对使用runnable的处理程序进行处理,会发生什么? [英] What will happen if I use try catch and finally for handler using runnable?

查看:204
本文介绍了如果我使用try catch并最终对使用runnable的处理程序进行处理,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码在一定的时间间隔后每次完成一些工作,并在"finally"子句中使用发布延迟,并撤消runnable.这是代码.

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                // do somthing
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                handler.postDelayed(this, 60000);
            }
        }
    };
    handler.postDelayed(runnable, 60000); 

handler.postDelayed(runnable, 60000);将运行两次或一次.

解决方案

要视情况而定!

  • 第一件事

每个try/catch/finally块如何正常或突然完成?

try块退出时,将执行finally块总是 ^ "

这确保即使发生意外异常也要执行finally块.

上面的

^ 异常-如果jvm退出或线程被杀死,最终可能不会执行

有关详细信息,请参见Java规范:
https://docs.oracle .com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2

  • 第二件事

处理程序post/postDelayed方法如何执行将通过?还是失败?出于某种原因-如果消息未放入消息队列中,可能会在第二秒发生-发生故障时,通常是因为处理消息队列的循环程序正在退出.

,但您的语句很可能会进行INFINITE循环

** ps.您需要在try块中引发异常或删除catch块(因为try {} finally {})可以不存在" catch存在,但是catch块中的代码没有引发任何异常将使编译器抱怨(代码将无法编译)

如果您想循环n次+1 ,则需要在Runnable run()方法中的postDelayed之前添加一些条件

在您的情况下执行代码流:

  1. 可运行定义之外的最后一行的
  2. postDelayed方法
  3. 可运行的执行者:
    • 尝试块的开始
    • 有或没有通过捕获
    • 在可运行的run()方法中通过带有postDelayed的finally块-wich将可运行的消息放置在消息队列中,以便在主线程上延迟执行
  4. 然后在2上进行无限循环

所以我应该删除run()方法的最后一个postDelay来实现postDelay在一个循环中只运行一个. – AndroidMob

您可以这样写:

    final Handler handler = new Handler();
    handler.post(new Runnable() {

        // this int will also be passed to method post delayed 
        // as "this" keyword applies to Anonymous Class 
        // which body contains everything between brackets of new Runnable() { ... }
        int withThis = 1;

        @Override
        public void run() {
            handler.postDelayed(this,1000);
        }
    });

那我应该在哪里调用方法来完成某事呢?在run()方法中..? – AndroidMob

这也取决于您要实现的目标:

示例

    handler.post(new Runnable() {

        int counter = 0;

        @Override
        public void run() {

            boolean wasPlacedInQue = false;

            doPreMethod();

            if(counter =< 10) { 
                  doMethod();
                  wasPlacedInQue = handler.postDelayed(this,1000);
            }

            if(wasPlacedInQue) { 
                  counter++; 
                  doPostyMethod(); 
            } else doFailureMethod();

        }

run()方法中的代码在所谓的UI线程上同步执行-我的主线程执行您的应用程序,请参见:

https://developer.android.com/training/multiple-线程/communicate-ui.html

如果您有兴趣进一步研究,我为您制作了一个完整的工作示例:

https://gist.github.com/c3ph3us/7d237d540e60597369856cb1fa652a23

I use the code below for getting some work done everytime after some time interval, and using post delay in 'finally' clause and oustide of runnable. Here is the code.

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                // do somthing
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                handler.postDelayed(this, 60000);
            }
        }
    };
    handler.postDelayed(runnable, 60000); 

handler.postDelayed(runnable, 60000); will run two times or a single time.

解决方案

it depends!

  • first matter

how the each try / catch / finally block completes normally or abruptly?

the finally block "always ^" executes when the try block exits

This ensures that the finally block is executed even if an unexpected exception occurs.

^ exception from above - finally may not be executed if jvm exits or thread gets killed

for details see java specs:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2

  • second matter

how Handler post/postDelayed method executes will pass? or fails? for some reason - for second this may happen if message was not placed in to the message queue - on failure, usually because the looper processing the message queue is exiting.

but most likely your statement will make a INFINITE loop

** ps. you need to throw an exception in try block or remove catch block (as try{} finally{} ) can exist "without" catch but code in catch block without rising any exception will make a compiler complain (code will not compile)

if you want loop n-times+1 you need to add some condition before postDelayed in Runnable run() method

in your case code flow execution:

  1. postDelayed method from last line outside definition of runnable
  2. execution of runnable by:
    • start of try block
    • with or without passing catch
    • through finally block with postDelayed in runnable run() method - wich will place runnable in message que for delayed execution on main thread
  3. then infinite loop on 2

so should i remove last postDelay out side of run() method to achieve postDelay run only ones in one loop. – AndroidMob

you can write this in such way:

    final Handler handler = new Handler();
    handler.post(new Runnable() {

        // this int will also be passed to method post delayed 
        // as "this" keyword applies to Anonymous Class 
        // which body contains everything between brackets of new Runnable() { ... }
        int withThis = 1;

        @Override
        public void run() {
            handler.postDelayed(this,1000);
        }
    });

so where should i call method to get done somthing ? in run() method..? – AndroidMob

this also depends what you want to achieve:

example

    handler.post(new Runnable() {

        int counter = 0;

        @Override
        public void run() {

            boolean wasPlacedInQue = false;

            doPreMethod();

            if(counter =< 10) { 
                  doMethod();
                  wasPlacedInQue = handler.postDelayed(this,1000);
            }

            if(wasPlacedInQue) { 
                  counter++; 
                  doPostyMethod(); 
            } else doFailureMethod();

        }

the code in run() method is executed synchronously on so called UI Thread - i'ts main thread which executes your application see:

https://developer.android.com/training/multiple-threads/communicate-ui.html

if you are interested to study it more I have made a full working example for you:

https://gist.github.com/c3ph3us/7d237d540e60597369856cb1fa652a23

这篇关于如果我使用try catch并最终对使用runnable的处理程序进行处理,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆