I currently have a tread that starts when you press a button on a GUI, this thread basicly starts to download files, but I wanted to implement that you can stop the tread, which worked fine with t.suspend();
but it is deprecated, so I tried to use t.wait();
and t.notify();
, and the problem with this is that the wait one throws an exception "Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException" every time I try to pause it.
Start Download Button:
t = new TestTread();
t.start();
Pause:
try {
t.wait();
} catch (InterruptedException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
Continue:
t.notify();
The Thread Class
public class DownloaderThread extends Thread{
@Override
public void run(){
Download();
}
public void Download() {
URL url = new URL(ftpUrl);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
}
The download works fine, its download the file without any errors, its just I cannot stop the thread with t.wait. Am I doing something wrong or am I implementing the wait in a wrong way?