暂停所有线程:使用Threads – ms警告ms

我有2个Thread进行一些网络计算。 当我运行我的应用程序并在启动我的第二个Thread我得到一个:

Suspending all threads took: ms警告后跟:

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警告。

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

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; } ... } 

基本上是this.getLyricsPlanetDotComResults(MainActivity.searchedLyrics); 第二个Thread中的方法一般是做很多互联网工作和计算。 比准确的网络更多的计算。

所以我想我得到了那些警告,因为第二个Thread太“忙”了? 或者也许只是我使用onCreate()方法和onStart()方法的活动生命周期的实现是错误的?

毋庸置疑,我没有得到我想要的输出,虽然我调试了应用程序并逐步完成了第二个Thread并且它完美地工作 。 所以,它必须与我的Activity的实现有关。

第一行基本上是说垃圾收集器(GC)决定它需要暂停所有线程来完成它的一些工作(例如重定位变量),这需要一些时间。 通常那里有一个数字。

第二行是收集的结果。 它释放了相当多的内存,现在你有1/3的空闲。 它要求你的应用程序暂停2毫秒,总共花费127毫秒收集垃圾。

GC本身并不坏。 但是,如果你一直在做这件事,要么你正在做一些需要大量内存的事情,要么你做的事情效率低下。 重字符串解析,特别是像JSoup这样的东西,可以导致很多小对象(主要是字符串)真正需要在像手机这样的小型存储设备上快速清理,所以在这里看到它并不奇怪。

基本上,这只是一个问题,如果你从GC得到性能打嗝或者你在某个时候要去OOM。 如果这些都没有发生,我也不会担心这一点。