最近做个程序,要利用webservice 从一个服务器上下载文件,client用的是Axis2。
不用线程速度有点慢,于是改为多线程来实现,每个线程只下5MB数据,根据偏移值获取不同的byte[],然后组装成一个整体的文件。(不支持断点续传)
现有需求,在下载过程中,如果取消下载,如何才能安全关闭正在下载的线程,比如一个线程已下载了1MB,还4MB没下载,此时要取消下载,这个怎么才能把线程停下来?我试了interrupt,interrupt 后线程还是在下载。如果用stop(已经不推荐使用了),stop程序就会卡住(奇怪的问题). 求教各位大虾怎么才能安全停住此线程?run方法
public void run(){
byte[] block_bytes=Axis2SoapManager.getBlocks(offset,length);
FileManager.writeFile(offset,block_bytes);
Thread.sleep(100);
}catch(InterruptedException iex){
//
}catch(IOException ioe){
//
}
不用线程速度有点慢,于是改为多线程来实现,每个线程只下5MB数据,根据偏移值获取不同的byte[],然后组装成一个整体的文件。(不支持断点续传)
现有需求,在下载过程中,如果取消下载,如何才能安全关闭正在下载的线程,比如一个线程已下载了1MB,还4MB没下载,此时要取消下载,这个怎么才能把线程停下来?我试了interrupt,interrupt 后线程还是在下载。如果用stop(已经不推荐使用了),stop程序就会卡住(奇怪的问题). 求教各位大虾怎么才能安全停住此线程?run方法
public void run(){
byte[] block_bytes=Axis2SoapManager.getBlocks(offset,length);
FileManager.writeFile(offset,block_bytes);
Thread.sleep(100);
}catch(InterruptedException iex){
//
}catch(IOException ioe){
//
}
这句代码是获取返回值的,运行后只有返回了才能继续。
byte[] block_bytes=Axis2SoapManager.getBlocks(offset,length);
http://zheng12tian.iteye.com/blog/991484
不要使用new Thread(new Runnable()) 这种方法启动。
提交到一个 ExecutorService.class XXX implements Runnable{...}ExecutorService service = Executors.newCachedThreadPool();Future<?> future = service.submit(new XXX());// 放弃
future.cancel(true);service.shutdown();
jdk从1.5开始已经增加了java.util.concurrent这个并发的专用类包,不知道为什么都不去用,非要自己写多线程去,还写不明白
package futuretest;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;public class FutMain
{ /**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub try
{
ExecutorService service = Executors.newCachedThreadPool();
Future<?> future = null;
try
{
future = service.submit(new FutureThread());
int i = 0;
while (!future.isDone())
{
if (i > 5)
{
future.cancel(true);
}
Thread.sleep(500);
System.out.println("私有账户计算未完成继续等待..."); i++;
}
System.out.println("done..?"+future.isCancelled());
}
catch (InterruptedException e)
{
// TODO: handle exception
// 放弃
future.cancel(true);
System.out.println("set cancel");
}
finally
{
System.out.println("shut down pool");
service.shutdown();
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}package futuretest;import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;public class FutureThread implements Runnable
{ @Override
public void run()
{
doWork();
} Integer totalMoney; void doWork()
{
// TODO Auto-generated method stub
try
{
boolean flag = false;
Thread.sleep(1000);
int k = 1;
while (!flag)
{
//
// do something here
k++;
if (k < 5) System.out.println("aa:" + k);
}
totalMoney = new Integer(new Random().nextInt(10000));
System.out.println("您当前有" + totalMoney + "在您的私有账户中"); }
catch (InterruptedException e)
{
// TODO: handle exception
System.out.println("InterruptedException in callable");
} }}
{
//
// do something here
k++;
if (k < 5) System.out.println("aa:" + k);
}
看了下源码,FutureTask的cancel也是打断线程吧
if (mayInterruptIfRunning) {
Thread r = runner;
if (r != null)
r.interrupt();
}