为什么我的主题冻结UI线程? [英] Why does my Thread freeze the UI-Thread?

查看:177
本文介绍了为什么我的主题冻结UI线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能看着办吧。

有关什么都原因,该线程的code实际上是在UI线程上运行。
如果我断点它UI停止。还是睡了吧,UI停止。因此网络活动是不允许的作为其对UI主题。

香港专业教育学院不使用异步任务,因为我不知道它循环的正确方法。 (调用它的一个新的实例在 onPostExecute 好像不好的做法,又仿佛异步是一次性的任务。

我扩展Thread。

 公共类SyncManager继承Thread {公共SyncManager(上下文的背景下){
    SDB =新SyncManagerDBHelper(背景);
    mContext =背景;
}@覆盖
公共无效的run(){    而(国== RUNNING){
        尝试{
            SyncRecords(); //此处断点= UI冻结。
        }赶上(例外五){
            e.printStackTrace();
        }        尝试{
            视频下载(10000); //所以也导致UI冻结。
        }赶上(InterruptedException的E){
            e.printStackTrace();
        }
    }
} 公共无效startThread(){
    Log.i(SyncManager,开始调用);    如果((状态== PAUSED ||状态== STOPPED)及&放大器;!this.isAlive())
    {
        状态=运行;
        运行();
    }
}

和从我的活动我称之为

  SM =新SyncManager(本);
  sm.startThread();


解决方案

您应该使用 Thread.start(); 来启动线程。
据我知道的调用运行是德precated - 不应再使用。此外,它的简单的不良作风。

这是因为呼叫 的run() 不创建一个新的线程,只叫 Thread.start()一样。所以,你的用户界面冻结,因为你并没有真正建立在主线程中一个新的线程和你的日常运行。

更改 startThread()方法下面,然后它应该工作:

 公共无效startThread(){    Log.i(SyncManager,开始调用);    如果((状态== PAUSED ||状态== STOPPED)及&放大器;!this.isAlive())
    {
        状态=运行;
        开始(); //使用的start()代替的run()
    }
}

在这里阅读更多的的具体信息:

<一个href=\"http://javarevisited.blogspot.co.at/2012/03/difference-between-start-and-run-method.html\">http://javarevisited.blogspot.co.at/2012/03/difference-between-start-and-run-method.html

I cant figure it out.

For what ever reason, this thread's code is actually running on the UI thread. If i break point it the UI stops. or Sleep it, UI stopped. and hence network activity is unallowed as its on the "ui" thread.

Ive not used Async task, because I am unaware of the proper way of looping it. (calling a new instance of it in onPostExecute seems like bad practice, and as if async is for one off tasks.

I extend Thread.

public class SyncManager  extends Thread {

public SyncManager(Context context){
    sdb = new SyncManagerDBHelper(context);
    mContext = context;     
}

@Override
public void run() {     

    while(State == RUNNING) {
        try{
            SyncRecords();   // Break point here = UI freeze.
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Thread.sleep(10000); // So also causes UI freeze.
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 public void startThread() {
    Log.i("SyncManager", "start called");

    if((State == PAUSED || State == STOPPED) && !this.isAlive() )
    {
        State = RUNNING;
        run();      
    }   
}   

ANd from my Activity I call

  sm = new SyncManager(this);
  sm.startThread();

解决方案

You should use Thread.start(); to start the thread. As far as I know calling "run" is deprecated - should no longer be used. Furthermore, its simply "bad style".

This is because calling run() does not create a new Thread, only calling Thread.start() does. So your UI freezes because you do not actually create a new Thread and your routine runs on the main thread.

Change your startThread() method to the following and it should then work:

public void startThread() {

    Log.i("SyncManager", "start called");

    if((State == PAUSED || State == STOPPED) && !this.isAlive() )
    {
        State = RUNNING;
        start();  // use start() instead of run()
    }   
}

Read here for more specific information:

http://javarevisited.blogspot.co.at/2012/03/difference-between-start-and-run-method.html

这篇关于为什么我的主题冻结UI线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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