QBugHunter
V2EX  ›  Android

这种情况下 EventBus 的函数还能执行完吗?

  •  
  •   QBugHunter · Dec 29, 2020 · 12448 views
    This topic created in 2015 days ago, the information mentioned may be changed or developed.

    有个类继承自 Thread

    class MyThread extends Thread{
        @Override
        public void run(){
            while(online){
                doSomehing();
            }
        }
    };
    
    public void finishThread(){
        online = false;
    }
    
    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void printLog(DeviceLog deviceLog){
        writeLog(deviceLog);
    }
    

    对于这样一个线程类,在运行过程中设备其他 Activity 有代码

    EventBus.getDefault().post(new DeviceLog());
    

    然后此时线程开始执行 writeLog()函数,假设改函数需要执行比较长的时间,然后在这段时间内,调用了线程的 finishThread()函数,此时线程 run()函数结束,但 wrtiLog()函数尚未执行完成,那这种情况下,能否保证 wrtiLog()函数执行完?

    1 replies    2020-12-30 00:29:48 +08:00
    kzaemrio
        1
    kzaemrio  
       Dec 30, 2020
    不太看得懂,感觉描述不全面

    假设这个 eventbus 正确地被注册,正确地收到了事件,那么 writeLog(deviceLog) 会运行在 eventBus.getExecutorService() 上,和这个 MyThread 应该无关
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1002 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 22:02 · PVG 06:02 · LAX 15:02 · JFK 18:02
    ♥ Do have faith in what you're doing.