Java或者Android開(kāi)發(fā)中經(jīng)常要中斷線(xiàn)程侯繁,這里總結(jié)下中斷正在運(yùn)行的線(xiàn)程
方法一:
設(shè)置標(biāo)記位置flag
public class InterruptThreadTest {
public static void main(String[] args) {
InterruptThread t=new InterruptThread();
t.start();
try {
Thread.sleep(5000);
t.flag=false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class InterruptThread extends Thread{
public boolean flag=true;
@Override
public void run() {
super.run();
while (flag){
System.out.println("run.....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
方法二:
使用java提供的中斷的方法interrupt()
/**
* <描述>
*
* @author levi
* @email lilinwei@xiaoyouzi.com
* @date: 2018/3/7
* @version: v1.0
*/
public class InterruptThreadTest2 {
public static void main(String[] args) {
InterruptThread t=new InterruptThread();
t.start();
try {
Thread.sleep(5000);
t.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static class InterruptThread extends Thread{
@Override
public void run() {
super.run();
while (true){
System.out.println("run.....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("InterruptedException.....");
e.printStackTrace();
break;//線(xiàn)程異常中斷線(xiàn)程
}
}
}
}
}
方法三:利用守護(hù)線(xiàn)程
既在創(chuàng)建線(xiàn)程中創(chuàng)建一個(gè)守護(hù)線(xiàn)程,如果創(chuàng)建線(xiàn)程結(jié)束則守護(hù)線(xiàn)程也妥畏。方法三的使用場(chǎng)景模式,如:一個(gè)線(xiàn)程正在拷貝一個(gè)很大的文件或者正在網(wǎng)絡(luò)請(qǐng)求襟沮,結(jié)束而且耗時(shí)超出預(yù)期诬像,這時(shí)候又想中斷它。如果使用方法一&方法二已經(jīng)不起使用了畏梆。這時(shí)候就可以使用方法三:
/**
* <描述>
*
* @author levi
* @email lilinwei@xiaoyouzi.com
* @date: 2018/3/7
* @version: v1.0
*/
public class InterruptThreadTest3 {
public static void main(String[] args) {
InterruptThreadTest3 InterruptThread=new InterruptThreadTest3();
long startTime=System.currentTimeMillis();
InterruptThread.execute(()->{
try {
System.out.println("在操作一個(gè)很大很大的文件....");
Thread.sleep(50_000);
} catch (InterruptedException e) {
System.out.println("守護(hù)線(xiàn)程結(jié)束....");
e.printStackTrace();
}
});
InterruptThread.shutDown(5_000);
long endTime=System.currentTimeMillis();
//在統(tǒng)計(jì)線(xiàn)程工作時(shí)間時(shí)您宪,可能會(huì)比實(shí)際使用的時(shí)間長(zhǎng)一些,因?yàn)樵趎ew,start的過(guò)程中消耗了一些時(shí)間
System.out.println("工作了...>"+(endTime-startTime));
}
public Thread serviceThread;
public boolean finished=false;
public void execute(Runnable task){
serviceThread=new Thread(()->{
Thread wokerTask=new Thread(task);
wokerTask.setDaemon(true);
wokerTask.start();
try {
wokerTask.join();//此線(xiàn)程等待子線(xiàn)程執(zhí)行完時(shí)才退出
finished=true;
} catch (InterruptedException e) {
System.out.println("創(chuàng)建線(xiàn)程中斷(結(jié)束)");
}
});
serviceThread.start();
}
//設(shè)置超時(shí)時(shí)間
public void shutDown(long minlls){
long currentTime=System.currentTimeMillis();
while (!finished){//標(biāo)記線(xiàn)程還沒(méi)結(jié)束
if(System.currentTimeMillis()-currentTime>minlls){
//超時(shí)了結(jié)束線(xiàn)程
serviceThread.interrupt();
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;//中斷當(dāng)前監(jiān)控線(xiàn)程
}
}
}
}