我正在下载一组图片。所以我有一个带静态方法的静态类,我用它来启动新的主线程。在主线程中,我创建了新的工作线程,为我下载图像,这在主线程的run方法中发生。在工作线程完成其任务后,它将被删除。
经过一段时间后,我正在导航到我的应用程序中的其他页面,该页面调用相同的静态方法,再次调用相同的进程。我将请求添加到此主线程类中的向量队列,但是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();
}
}
主线程类
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);
}
}
答案 0 :(得分:3)
在退出应用程序之前,请勿致电ImageDownloader.stop()
。这将使run
方法保持运行/等待。