2

I use a worker thread to read text from a url. My thread is as follow. In the first time running, I am sure thread running is finished as I can check sdcard_readstr is null. In the second time running, when I call thread_download.start();, then the program crashed. What could be wrong? Thanks

public class DownloadingThread extends AbstractDataDownloading {


      @Override
      public void doRun() {
        // TODO Auto-generated method stub
        try {
                // Create a URL for the desired page
                URL url = new URL(SDcard_DetailView.textfileurl);

                    // Read all the text returned by the server
                BufferedReader in = new BufferedReader(new  
                    InputStreamReader(url.openStream()));

                do{
                     sdcard_readstr = in.readLine();
                }while(sdcard_readstr!=null);
                in.close();

                } catch (MalformedURLException e) {
                } catch (IOException e) {
        }
    }
}


public abstract  class AbstractDataDownloading extends Thread{
       private final Set<ThreadCompleteListener> listeners
          = new CopyOnWriteArraySet<ThreadCompleteListener>();
       public final void addListener(final ThreadCompleteListener listener) {
            listeners.add(listener);
       }
       public final void removeListener(final ThreadCompleteListener listener) {
            listeners.remove(listener);
       }
       private final void notifyListeners() {
           for (ThreadCompleteListener listener : listeners) {
                 listener.notifyOfThreadComplete(this);
           }
       }
       @Override
       public final void run() {
          try {
                 doRun();
          } finally {
                 notifyListeners();
          }
       }
      public abstract void doRun();


}

EDIT1: In my thread complete notification, I use runOnUiThreadto use the UI components. Is that causing problem?

public void notifyOfThreadComplete(Thread thread) {
        // TODO Auto-generated method stub
        if(downloadingStopbuttonispressed == false){//background process completed

            textfileurl = null;

            this.runOnUiThread(new Runnable() {
                public void run() {
                    Wifibutton = (Button) findViewById(R.id.Wifiscanning); 
                    Wifibutton.setText("Load another day's data");
                    final MenuItem refreshItem = optionsMenu.findItem(R.id.airport_menuRefresh);
                    refreshItem.setActionView(null);
                }
            });


        }
    }

I called thread start in onResume() as

@Override
    protected void onResume() {
        super.onResume();
        if(textfileurl != null){
            Wifibutton.setText("Stop Data Loading");
            buttonStatus = "loading";           
            setRefreshActionButtonState(true);          
            thread_download.start();            
        }
    }

EDIT2: My LogCat image is attached. enter image description here

batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

3

My solution is here . I can't reuse the same instance of the Thread object in the second time. I need to create a new instance to call the Thread in the second time. So Thread is suitable for single time running process, for multiple time running process I should use AsyncTask. Even AsyncTack is only for one time execution and for multiple time execution, we should use as new MyAsyncTask().execute(""); I don't understand why people downvote with no reason given. I couldn't find the link in my first search.

Community
  • 1
  • 1
batuman
  • 7,066
  • 26
  • 107
  • 229