暂停所有线程:ms使用线程 - Android的警告 [英] Suspending all threads took: ms warning using Threads - Android

查看:161
本文介绍了暂停所有线程:ms使用线程 - Android的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 Thread 来做一些网络计算。
当我运行我的应用程序,并开始我的第二个线程后,我得到一个:



暂停所有线程:ms
警告后跟:

背景粘性并发标记扫描GC释放246745(21MB)AllocSpace对象,169(6MB)LOS对象,33%免费,31MB / 47MB,暂停1.972ms,总计127.267ms 警告。

有时我会得到这两个警告,其他时候我会收到很多这两个警告,直到我决定终止应用程序运行。在这一点上,它只是运行主要 Thread ,基本上什么都不做。以下是相关的代码:

MainActivity.java

  protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//通过线程获取html页面
this.getHtmlPageThread = new GetHtmlPageThread(URL_STRING);
this.getHtmlPageThread.start();

//将在Web上搜索数据的线程
this.getDataFromTheWebThread = new GetDataFromTheWebThread();

//搜索按钮点击侦听器
searchButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//获取搜索歌词
searchingLyrics = inputEditText.getText()。toString();

informUserAboutConnectionToTheNet();

//开始通过线程在网上搜索数据
getDataFromTheWebThread.start();

if(!getDataFromTheWebThread.isAlive())
{
printMap (MainActivity.matchResultMap);
}
}
}); //搜索结束按钮点击侦听器

printMap(MainActivity.matchResultMap);

} //结束onCreate()方法

保护void onStart()
{
super.onStart(); $!
$ b如果(!this.isParseSucceeded())//连接到网络失败
{
if(!getHtmlPageThread.isAlive())//如果线程不活动,启动它。
{
getHtmlPageThread.start(); //尝试再次连接
this.informUserAboutConnectionToTheNet();


if(!this.isParseSucceeded())
{
super.onStart(); //调用onStart()方法
}

} //结束onStart()方法

GetHtmlPageThread.java

  public class GetHtmlPageThread extends Thread 
{
私人字符串网址;

public GetHtmlPageThread(String url)
{
this.url = url;
}

@Override
public void run()
{
try
{
MainActivity.htmlPage.setHtmlDocument(this .getParsedDocument(this.url));
if(MainActivity.htmlPage.getHtmlDocument()!= null)
{
MainActivity.parsedSucceeded = true; //解析成功
}
else
{
MainActivity.parsedSucceeded = false; //解析失败
}
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

/ **
*返回url参数的文档对象。
*如果连接失败,则返回null。
*
* @param url Url解析
* @return url的文档。
*
* /
public Document getParsedDocument(String url)
{
try
{
return Jsoup.connect(url).get ();

catch(IOException e)//出错
{
e.printStackTrace();
}

返回null; //无法连接到网址
}

}

GetDataFromTheWeb.java

  public class GetDataFromTheWebThread extends Thread 
{
public static boolean isFinished = false; //假 - 线程仍在运行。 True - 线程无效

@Override
public void run()
{
GetDataFromTheWebThread.isFinished = false;
试试
{
this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics); //互联网计算的方法
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
GetDataFromTheWebThread.isFinished = true;
}
...
}

基本上 this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics);第二个 Thread 中的方法正在进行大量的互联网工作和计算。更准确地说,计算量比网络资料要多。所以我想我得到了这些警告,因为第二个 Thread 也是如此忙?或者,也许只是我的 onCreate()方法和 onStart()方法的活动生命周期的实现是错误的?

不用说,我没有得到我想要的输出,尽管我调试了应用程序并逐步完成了第二个 Thread ,它的工作原理完美。所以再一次,它是我的 Activity 的实现。

第一行基本上是说垃圾收集器(GC)决定需要暂停所有线程来完成一些工作(比如重定位变量),这需要花费一些时间。通常在那里有一个数字。 第二行是收集的结果。它释放了相当多的记忆,现在你有三分之一的空闲空间。它需要你的应用程序暂停2ms,总共花费127毫秒收集垃圾。



GC本身并不差。但是如果你一直在做这些事情,无论你是在做一些需要大量内存的事情,或者你做的事情效率低下。特别是像JSoup这样的重字符串解析可能会导致大量小对象(主要是字符串)被真正需要在像手机这样的小存储设备上快速清理,因此在这里看到这并不奇怪。 >

基本上,这只是一个问题,如果您从GC获取性能问题或者在某些时候要执行OOM。如果这两者都没有发生,我不会担心这一点。


I have 2 Threads that make some network computation. When I run my app and after starting my second Thread I get a:

Suspending all threads took: ms warning followed by:

Background sticky concurrent mark sweep GC freed 246745(21MB) AllocSpace objects, 169(6MB) LOS objects, 33% free, 31MB/47MB, paused 1.972ms total 127.267ms warning.

Sometimes I get just those 2 warnings and other times I get a lot of those 2 warnings until I decide to terminate the app running. At this point, it's just running the main Thread and basically doing nothing. Here is the relevant code:

MainActivity.java:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting html page through a thread
    this.getHtmlPageThread = new GetHtmlPageThread(URL_STRING);
    this.getHtmlPageThread.start();

    // The thread that will search the web for data
    this.getDataFromTheWebThread = new GetDataFromTheWebThread();

    // Search button click listener
    searchButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // Get the searched lyrics
            searchedLyrics = inputEditText.getText().toString();

            informUserAboutConnectionToTheNet();

            // Starting to search the web for data through a thread
            getDataFromTheWebThread.start();

            if (!getDataFromTheWebThread.isAlive())
            {
                printMap(MainActivity.matchResultMap);
            }
        }
    }); // End of search button click listener

    printMap(MainActivity.matchResultMap);

} // End of onCreate() method

protected void onStart()
{
    super.onStart();

    if (!this.isParseSucceeded()) // Connection to net failed
    {
        if (!getHtmlPageThread.isAlive()) // If the thread is not alive, start it.
        {
            getHtmlPageThread.start(); // Try to connect again
            this.informUserAboutConnectionToTheNet();
        }
    }
    if (!this.isParseSucceeded())
    {
        super.onStart(); // Call onStart() method
    }

} // End of onStart() method

GetHtmlPageThread.java:

public class GetHtmlPageThread extends Thread
{
    private String url;

    public GetHtmlPageThread(String url)
    {
        this.url = url;
    }

    @Override
    public void run()
    {
        try
        {
            MainActivity.htmlPage.setHtmlDocument(this.getParsedDocument(this.url));
            if (MainActivity.htmlPage.getHtmlDocument() != null)
            {
                MainActivity.parsedSucceeded = true; // Parsed succeeded
            }
            else
            {
                MainActivity.parsedSucceeded = false; // Parsed failed
            }
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Returns the document object of the url parameter.
     * If the connection is failed , return null.
     *
     * @param url Url to parse
     * @return The document of the url.
     *
     */
    public Document getParsedDocument(String url)
    {
        try
        {
            return Jsoup.connect(url).get();
        }
        catch (IOException e) // On error
        {
            e.printStackTrace();
        }

        return null; // Failed to connect to the url
    }

}

GetDataFromTheWeb.java:

public class GetDataFromTheWebThread extends Thread
{
    public static boolean isFinished = false; // False - the thread is still running. True - the thread is dead

    @Override
    public void run()
    {
        GetDataFromTheWebThread.isFinished = false;
        try
        {
            this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics); // Method for internet computations
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        GetDataFromTheWebThread.isFinished = true;
    }
    ...
}

Basically the this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics); method in the second Thread is doing a lot of the internet work and computations in general. More computations than net stuff to be exact.

So I guess I got those warnings because the second Thread is too "Busy"? Or maybe just my implementation with the activity life cycle with the onCreate() method and onStart() method are wrong?

Needless to say, I don't get the output I want, though I debugged the app and stepped through the second Thread and it works Perfectly. So again, it got to be something with my Activity's implementation.

解决方案

The first line is basically saying that the garbage collector (GC) decided it needed to suspend all threads to do some of its work (such as relocating variables) and that took some time. Normally there's a number there.

The second line is the result of the collection. It freed a fair amount of memory, and you now have 1/3 of your heap free. It required your app to be paused for 2ms, and in total spent 127 ms collecting garbage.

GC in and of itself isn't bad. But if you're doing it all the time, either you're doing something with lots of memory required or you're doing something inefficiently. Heavy string parsing, especially for something like JSoup, can cause lots of small objects (mainly strings) to be made that really required cleanup quickly on a small memory device like a phone, so it isn't surprising to see here.

Basically, this is only a problem if you're getting performance hiccups from GC or if you're going OOM at some point. If neither of those are happening, I wouldn't worry about this yet.

这篇关于暂停所有线程:ms使用线程 - Android的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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