减轻 Android UI 线程的繁重计算

发布于 2024-12-23 19:00:59 字数 813 浏览 1 评论 0原文

我的 Android 应用程序使用特别大的计算,导致系统不断崩溃,因为它位于 Activity 中的 UI 线程上。 我对多线程没什么信心,所以我想获得一些关于如何正确执行多线程的提示。 这就是我

class ActivtyName extends Activity{
boolean threadcomplete = false;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//stuff

Runnable newthread = new Runnable(){

            @Override
            public void run() {
                doBigComputation();
                threadcomplete=true;
            }

        };

        newthread.run();

        boolean b = true;
        while(b){
            if(threadcomplete){
                b=false;
                startTheApp();
            }
        }

}
}

现在所拥有的,我很确定我所做的不是“正确的”。 (虽然它似乎有效。系统不会崩溃)。基本上,我不确定如何在没有这个布尔值、threadcomplete 的情况下告诉 UI 线程 newthread 已完成计算。有没有“正确”的方法来做到这一点?

My Android app employs a particularly big computation which keeps crashing the system because it is on the UI thread in the Activity.
I have little confidence in multithreading and so I want to get some tips on how to do it correctly.
This is what I have

class ActivtyName extends Activity{
boolean threadcomplete = false;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//stuff

Runnable newthread = new Runnable(){

            @Override
            public void run() {
                doBigComputation();
                threadcomplete=true;
            }

        };

        newthread.run();

        boolean b = true;
        while(b){
            if(threadcomplete){
                b=false;
                startTheApp();
            }
        }

}
}

Now, I am pretty sure what I have done is not "correct". (It seems to work though. The sistem doesn't crash). Basically, I'm not sure how the UI thread can be told that newthread has finished the computation without this boolean, threadcomplete. Is there a "correct" way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡水深流 2024-12-30 19:00:59

只是为了扩展 Marek Sebera 的评论,下面是有关如何在 AsyncTask 中实现这一目标的框架。

private class BigComputationTask extends AsyncTask<Void, Void, Void> {

  @Override
  protected void onPreExecute() {
    // Runs on UI thread
    Log.d(TAG, "About to start...");
  }

  @Override
  protected Void doInBackground(Void... params) {
    // Runs on the background thread
    doBigComputation();
  }

  @Override
  protected void onPostExecute(Void res) {
    // Runs on the UI thread
    Log.d(TAG, "Big computation finished");
    startTheApp();
  }

}

并称其为:

BigComputationTask t = new BigComputationTask();
t.execute();

Just to expand a bit on Marek Sebera's comment, here's the framework on how you would accomplish that in an AsyncTask.

private class BigComputationTask extends AsyncTask<Void, Void, Void> {

  @Override
  protected void onPreExecute() {
    // Runs on UI thread
    Log.d(TAG, "About to start...");
  }

  @Override
  protected Void doInBackground(Void... params) {
    // Runs on the background thread
    doBigComputation();
  }

  @Override
  protected void onPostExecute(Void res) {
    // Runs on the UI thread
    Log.d(TAG, "Big computation finished");
    startTheApp();
  }

}

And to call it:

BigComputationTask t = new BigComputationTask();
t.execute();
一身软味 2024-12-30 19:00:59

In addition to Marvin's answer, there's a good article on the Android developer site about precisely this.

耳根太软 2024-12-30 19:00:59

这不是启动线程的正确方法。你需要做:

    Runnable newthread = new Runnable(){

        @Override
        public void run() {
            doBigComputation();
            threadcomplete=true;
        }

    };

    Thread t = new Thread(newthread);
    newthread.start();

    boolean b = true;
    while(b){
        if(threadcomplete){
            b=false;
            startTheApp();
        }
    }

That's not the correct way to start a thread. You need to do:

    Runnable newthread = new Runnable(){

        @Override
        public void run() {
            doBigComputation();
            threadcomplete=true;
        }

    };

    Thread t = new Thread(newthread);
    newthread.start();

    boolean b = true;
    while(b){
        if(threadcomplete){
            b=false;
            startTheApp();
        }
    }
如梦 2024-12-30 19:00:59

当我想使用线程时,我几乎有一个很大的 while 循环。我设置了一个始终为真的布尔条件,除非我想停止它。

我使用 Thread (http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html) 并重新实现 stop 方法,因为它已被弃用。因此,在我的 stop 方法中,我可以为循环变量添加一个假值。

为了终止线程,我使用 t.stop()。所以你可以在你的活动中做到这一点。

如果你想知道 Thead 何时停止,可以使用 t.isAlive()。如果线程 t 已启动,isAlive 将返回 true。

When i want to use threads, I've almost a big while loop. I put a boolean condition who's always true, except when I want to stop it.

I use Thread (http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html) and I reimplement the stop method because it's deprecated. So in my stop method I can put a false value to the loop variable.

To terminate the thread, I use t.stop(). So you can di that in your activity.

If you want to know when a Thead stops, you can use t.isAlive(). If the thread t is started, isAlive will return true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文