Reputation: 15057
I am downloading a set of images. So I have a static class with a static method, which I use to start the new main thread. In the main thread, I have created new worker threads which download images for me which happens in the run method of the main thread. After worker thread finishes its task, it is deleted.
After some times, I am navigating to some other page in my application, which calls the same static method, the same process again. I am adding the request to vector queue in this main thread class, but the run method already got finished.
How do I make it run again?
Is this the correct way of approaching threads?
Static Method for a static class
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();
}
}
Main Thread class
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();
}
}
}
Custom Worker Thread class
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);
}
}
Upvotes: 0
Views: 163
Reputation: 4043
Don't call ImageDownloader.stop()
until you are exiting the application. This will keep the run
method running/waiting.
Upvotes: 3