Friday 15 July 2011

java - Multithread is stopped unexpectedly in Asynctask -


I am trying to scan all the files in my Android device. I used a multilevel class like this:

  Public class FileScanner {// Subfolders to the Private Final Quarter & lt; File & gt; ExploreList = new concurrent written questions & lt; File & gt; (); Private long file control = 0; & Lt; File & gt; ListFile = New Arrestist & lt; File & gt; (); Public Zero count () {fileCounter ++; } Received long public () {return.fileCounter; } Public listing & lt; File & gt; GetListFile () {return.listFile; } Int [] threads; Public FileScanner (int numberoffrades) {thread = new int [numberoffreads]; For (int i = 0; i & lt; thread height; i ++) {thread [i] = -1; }} Zero Scan (file file) {// Add one to the first list - Add list (file); For (int i = 0; i & lt; thread length; i ++) {file explorer explorer = new file explorer (i, it); Thread T = New Thread (Explorer); T.start (); } Thread waitToFinish = New Runnable () {@Override Public Zero Run () {Boolean working = true (working) {for work = false; (int i = 0; I & lt; threads. Length; i ++) {if (threads [i] == -1) {working = true break;}} {try thread.Sleep (1);} hold (interruptedException e) {e.printStackTrace ();} }}}); WaitToFinish.start (); } Public zero done (int id, int counter) {thread [id] = counter; } Public Boolean Fineish () (for (int i = 0; i & lt; thread lamps; i ++) {if (threads [i] == -1) {return false;}} true true;} class File Explorer Runable {Public Intit Counter = 0; Public FileScanner Owner; Private Ent ID; Public FileExplorer (int id, FileScanner owner) {this.id = id; This.owner = Owner;} @Override Public Zero Run () { Try {{file file = (file) owner.exploreList.remove () before trying {// list} (owner.exploreList.isEmpty ()!); If (file.exists ()) {if (! File.isDirectory ( )) {Count (); listFile.add (f L);} Else {// Add files to the queue file [arr arr = file.listFiles]; if (arr! = Null) {for (int i = 0; i & lt; arr.length; i ++ ) {Owner.exploreList.add (arr [i]);}}}}} hold (exception e) {e.printStackTrace (); // silent :)} {{Thread.sleep (1); } Grip (Interrupted e) e.printStackTrace (); }} Owner.done (id, counter); }}   

And I call it in my Asynctask:

Private class FetchResidualAsynctask extends AsyncTask {FileScanner fileMachine;

  @Override Protected Void Parikuute () {super.onPreExecute (); ListResidualFileTemp.clear (); ListResidualFileThumbnail.clear (); ListResidualAppAds.clear (); ListResidualAppLeftOvers.clear (); FindAllStorage (); For (int i = 0; i   

The problem is that the list of files is so useless. As I debug, each time I test, the results are different. First time it returns very small files (example: 160), the next big is (1200).

I think the FileScanner file Machine.scan () has not ended yet and the force has been closed to run DoInBackground.

Can anyone help me on this? Good question: Normally, it is not possible to stop a bunch of threads and somehow " Work "instead, you will have to create a pool of pre-defined thread threads, and when you have to work, a new outlet will have to be parceled out. At some point, a task that you want to run on the thread will wait, because there are no threads left, this expected behavior is to facilitate multiple thread usage, to first determine the maximum number of threads, create threads, And then only start working. The training class describes it in a few details.

No comments:

Post a Comment