Android runOnUiThread()使用Thread.sleep()时不执行

Android runOnUiThread()使用Thread.sleep()时不执行,android,multithreading,Android,Multithreading,我使用的代码如下所示: _thread = new Thread(){ @Override public void run() { try { while (true) { operate(); Thread.sleep(DELAY); }

我使用的代码如下所示:

_thread = new Thread(){
            @Override
            public void run() {
                try {
                    while (true) {
                        operate();
                        Thread.sleep(DELAY);
                    }
                } catch (InterruptedException e) {
                    // Doesn't matters...
                }
            }
  };
    // does things....
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // adds an ImageView to the screen
        }
    });
    // does other things...
操作函数如下所示:

_thread = new Thread(){
            @Override
            public void run() {
                try {
                    while (true) {
                        operate();
                        Thread.sleep(DELAY);
                    }
                } catch (InterruptedException e) {
                    // Doesn't matters...
                }
            }
  };
    // does things....
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // adds an ImageView to the screen
        }
    });
    // does other things...
归根结底,我想要实现的是一个偶尔发生一次的操作,而不会中断主线程和UI,就像一个游戏循环

在operate()运行的前2次中,它添加了ImageView,一切正常,但在2到3次之后,它停止添加ImageView,但UI仍像往常一样运行。当我调试这个问题时,我发现在3次之后,Runnable的run()方法不再被调用,甚至认为operate函数被调用了

(对我来说)最重要的是,当我拔掉线的时候。睡眠,一切正常(当然要快得多…)。我试着用一个很长的for循环(只是为了检查)来代替它,它成功了,但当然这不是解决问题的合适方法

我读到关于这个问题的文章,问这个问题的大多数人在主线程上做了一个thread.sleep或无限循环,但在我看来,我没有做这样的事情。许多人写道,应该用Handler.postdayed替换Thread.sleep。我试着去做,但没有成功,也许我做错了。我甚至尝试用我在互联网上找到的其他选项来替换runOnUiThread,但所有这些选项都给出了相同的结果。我试图替换将视图添加到活动中的方法,但所有方法都给出了相同的结果


等待对于此应用程序至关重要。我需要找到一种方法等待一段时间,然后在UI线程上执行一个函数,因为这个模式在我的应用程序中至少会返回几次。

听起来您需要延迟一段时间,以便在延迟一段时间后在UI线程上执行代码

建议的创建可运行文件的方法

private static class MyRunnable implements Runnable
{
    private WeakReference<MainActivity> mRef;

    // In here you can pass any object that you need.
    MyRunnable(MainActivity activity)
    {
        mRef = new WeakReference<MainActivity>(activity);
    }

    @Override
    public void run()
    {
        // Safety check to avoid leaking.
        MainActivity activity = mRef.get();
        if(activity == null)
        {
            return;
        }

        // Do something here.
        activity.doSomething();
    }
}
私有静态类MyRunnable实现Runnable
{
私人财富参考基金;
//在这里你可以传递任何你需要的物体。
MyRunnable(主活动)
{
mRef=新的WeakReference(活动);
}
@凌驾
公开募捐
{
//安全检查以避免泄漏。
MainActivity=mRef.get();
如果(活动==null)
{
返回;
}
//在这里做点什么。
活动。doSomething();
}
}

UI Runnable没有被执行可能有几个原因。可能是
activity
变量有问题,或者它引用的上下文不正确,或者正如您所说的
Thread.sleep()
可能会导致问题。此时,需要查看更多的代码部分以更好地解决问题

实现逻辑的更好方法是使用一个计划的
计时器
,而不是使用一个包含
Thread.sleep()
的无限循环。它将在后台线程中执行代码。然后使用
处理程序来更新UI,而不是
活动。runOnUiThread()
。下面是一个例子:

// Global variable within the activity
private Handler handler;

// Activity's onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
    handler = new Handler(getMainLooper());
    Timer timer = new Timer("ScheduledTask");
    // Timer must be started on the Main UI thread as such.
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            operate();
        }
    }, 0L, DELAY);
}

private void operate() {
    // does things in background....
    handler.post(new Runnable() {
        @Override
        public void run() {
            // adds an ImageView to the screen from within the Main UI thread
        }
    });
    // does other things in the background...
}

谢谢你回答我的问题!我确实想延迟,但我想重复,我的意思是每500毫秒做一件事。我怎么能用Handler解决它。延迟?没有什么能阻止你重新安排它。我已经更新了我的答案,以进一步帮助您。谢谢您的回答。Timer.scheduleAtFixedRate这个选项对我来说非常有用@萨皮尔莫萨科这个答案泄露了记忆。仅供参考。@Newtron实验室为无知感到抱歉,但是怎么做呢?