2013-05-30 63 views
0

我正在下载一组图像。所以我有一个静态方法,我用它来启动新的主线程。在主线程中,我创建了新的工作线程,它为我下载图像,这发生在主线程的run方法中。工作线程完成其任务后,它将被删除。如何处理线程

经过一段时间,我在我的应用程序中导航到其他页面,该页面调用相同的静态方法,同样的过程再次。我在这个主线程类中向vector队列添加了请求,但run方法已经完成。

如何让它再次运行?

这是接近线程的正确方法吗?

一个静态类

public class ImageLoader { 
    private static Vector requestQueue = new Vector(); 
    static ImageDownloader mThread ; 

    public static void loadImage(String url,ImageDownloadListener _listner){   
     CustomWorkerThread thread = new CustomWorkerThread(url, _listner); 
     if(mThread==null){ 
      mThread = new ImageDownloader(); 
      if(!mThread.isAlive()){ 
       mThread.start(); 
       } 

     } 
     ImageDownloader.loadImageThread(thread); 
    } 
    public static void closeThreads(){ 
     ImageDownloader.stopAllThreads(); 
    } 
} 

主要Thread类

public static ImageDownloadListener listner; 

static ImageDownloader mainThread; 

ImageDownloader(){ 
    mainThread = this; 
    this.start();  
} 

public void run(){ 
    System.out.println("Within the Run Method Of Main Thread size>>>>>"+requestQueue.size()); 
    while (true) { 
     if(_stop) 
      return ; 
     if(requestQueue.size()>1){   
      while(count<2){ 
       CustomWorkerThread threader = (CustomWorkerThread)requestQueue.elementAt(0); 
       requestinProgressQueue.addElement(threader); 
       Thread th = new Thread(threader); 
       th.start(); 
       count++; 
       requestQueue.removeElementAt(0); 
      } 
     }else{ 
      //mainThread.run(); 
      synchronized (requestQueue) { 
       try { 
        requestQueue.wait(1000); 
       } catch (InterruptedException e) { 
       } 
      } 
     } 
    } 
} 

public static void loadImageThread(CustomWorkerThread thread){ 
    System.out.println("Simple Counter>>>"+simplecount); 
    synchronized (requestQueue) { 
      requestQueue.addElement(thread); 
      requestQueue.notify(); 
    } 

    simplecount++;   
} 

public synchronized void stop(){   
    _stop = true;    
} 

public static void Reload(){ 
    if(requestQueue.size()>=1){   
     while(count<2){ 
      CustomWorkerThread threader = (CustomWorkerThread)requestQueue.elementAt(0); 
      requestinProgressQueue.addElement(threader); 
      Thread th = new Thread(threader); 
      th.start(); 
      count++; 
      requestQueue.removeElementAt(0); 
     } 
    } 

} 

public synchronized static void stopAllThreads(){ 
    if(requestQueue.size()>=1){ 
     for(int i=0;i<requestinProgressQueue.size();i++){ 
      CustomWorkerThread threaderinProgress = (CustomWorkerThread)requestQueue.elementAt(i); 
      threaderinProgress.stop = true; 
      threaderinProgress = null; 
     } 
     _stop = true; 
     requestQueue.removeAllElements(); 
    } 

} 

}

自定义工作线程类

public class CustomWorkerThread implements Runnable{ 

    public String url; 
    private boolean _stop; 
    HttpConnection connection; 
    ImageDownloadListener listener; 
    public boolean stop = false; 

    CustomWorkerThread(String _url, ImageDownloadListener _listener){ 
     System.out.println("On Creating CustomWorkerThread >>>>>>>>"+_url); 
     url = _url ; 
     listener = _listener; 
    } 

    public byte[] getBytesData(){ 
     try{ 
      MyConnectionFactory _factory = new MyConnectionFactory(); 
      ConnectionDescriptor con=_factory.getConnection(url); 
      connection=(HttpConnection) con.getConnection(); 
      System.out.println("connectionUrl:"+connection.getURL()); 
      byte [] _data=null; 
      InputStream is=null; 
      ByteArrayOutputStream byteArray=new ByteArrayOutputStream();    
      try {   
       int rc = connection.getResponseCode(); 
       if(rc == HttpConnection.HTTP_OK) {   
        is = connection.openInputStream(); 
        _data = new byte[10240*5]; 
        int bytesRead=0; 
        while ((bytesRead = is.read(_data))!= -1){ 
         byteArray.write(_data,0,bytesRead); 
        } 
        byte[] bData=byteArray.toByteArray();      
        return bData; 

       } 
      }catch (IOException e1) { 
       System.out.println("Exception in Reading Data"+e1); 
       stop = true; 
      }finally { 
       try { 
         if (is != null) is.close();   
         if (connection != null) connection.close(); 
        } catch (Exception e) { 
         stop = true; 
        } 
      } 
     }catch(Exception e){ 
      System.out.println("Exception in getting Server Data"+e); 
      stop = true; 
     } 
     return null; 
    } 

    public void run(){ 
     if(stop) 
      return; 
     byte[] image = this.getBytesData(); 
     listener.imageDownloaded(image); 
     System.out.println("Response Recieved From :>>>>>>>>"+url); 
     this.stop(); 
    } 

    public synchronized void stop(){ 
     stop = true; 
     ImageDownloader.count--; 
     ImageDownloader.requestinProgressQueue.removeElement(this); 
     ImageDownloader.Reload(); 
     System.out.println("On Stop CustomWorkerThread >>>>>>>>"+url); 
    }      
} 
+3

请向我们展示您的**代码**。这种描述不足以告诉你什么是错的。另外,你不能启动*“一个新的主线程”*。有一个“主”线程,黑莓会为你启动。您创建的所有线程都是工作线程。请更新您的描述,因为这相当混乱。谢谢。 – Nate

+0

更新的完整代码 – Rakesh

+3

这对我没有任何意义。你在'ImageLoader'类中声明'requestQueue',但是你可以在'ImageDownloader'类中使用它。这怎么样? ImageDownloader是一个内部类吗?您确实需要格式化此代码,因为它是不可读的。 [试试这个网站](http://www.prettyprinter.de/module.php?name=PrettyPrinter)。这段代码有很多很多问题。我想你会很难得到这个帮助,我很抱歉地说。 – Nate

回答

3

不要叫静态方法0直到您退出应用程序。这将保持run方法运行/等待。