runnig

Downloading thread

Nov 6th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1.  
  2. public class Downloader {
  3.         private enum State {STARTED, STOPPED, PAUSED};
  4.         private State mState = STOPPED;//initial state
  5.         //...initialization and stuff
  6.         private void synchronized getState() { return mState; }
  7.         private void synchronized setState(State newState) { mState = newState; }
  8.        
  9.         private Thread workerThread = new Thread(new Runnable {
  10.                 @Override
  11.                 public void run() {
  12.                         //main loop for downloading, managed by states
  13.                
  14.                         final int buffer_size = 8 * 1024;
  15.                         byte[] buffer = new byte[buffer_size];
  16.                         Task task = null;
  17.                        
  18.                         while( getState() != STOPPED)
  19.                         {                        
  20.                             while(getState() == PAUSED)
  21.                             {                                
  22.                                 // this thread must be notified when awaken
  23.                                 signal.wait();                                            
  24.                             }
  25.                            
  26.                             if(getState() != STARTED) { continue; }
  27.                            
  28.                             while(task == null)
  29.                             {
  30.                                 // this thread must be notified from outside
  31.                                 // when new tasks is added to the work queue
  32.                                 signal.wait();
  33.                                 task = getNextTaskFromServer();
  34.                             }                
  35.                            
  36.                             int bytesRead = task.input.read(buffer, buffer_size);
  37.                             if( -1 != bytesRead)
  38.                             {
  39.                                 task.output.write(buffer,0, bytesRead);
  40.                             }  
  41.                             else
  42.                             {
  43.                                 task.close();
  44.                                 task = null;
  45.                             }
  46.                         }
  47.                         Log.d("worker Thread is finished");
  48.                 }
  49.         });
  50.        
  51.         public synchronized void start() {
  52.                 //check current state first
  53.         assert(mState == STOPPED);
  54.                 mState = STARTED;
  55.                 workerThread.start();
  56.         }
  57.        
  58.         public synchronized void stop() {
  59.                 mState = STOPPED;
  60.                 signal.NotifyAll();
  61.         }
  62.        
  63.         public synchronized void pause() {
  64.         assert(mState == STARTED);
  65.                 mState = PAUSED;
  66.         }
  67.        
  68.         public synchronized void resume() {
  69.          assert (mState == PAUSED);
  70.                  mState = STARTED;
  71.                 this.NotifyAll();
  72.         }
  73.        
  74. }
Add Comment
Please, Sign In to add comment