isAlive()方法:判断当前的线程是否处于活动状态。
我们自定义的线程,在调用start()后就处于活动状态,直到线程编译完。
而main线程,一开始就处于活动状态,直到main线程编译完。
sleep()方法:让正在执行的线程暂停一段时间。
getId()方法:取得线程的唯一标识。
停止线程:
我用的是interrupt方法中断线程(已废弃stop方法)。
这里我介绍一下几个关键的方法:
1.interrupt():给当前线程附上一个停止标记;
package Java_test;
import java.io.File;
public class Test {
public static void main(String[] args) {
Mythread thread = new Mythread();
thread.start();
thread.interrupt();
}
}
class Mythread extends Thread {
int i;
public void run() {
// TODO Auto-generated method stub
System.out.println("begin");
for (i = 0; i < 10000; i++) {
System.out.println("i=" + i);
}
System.out.println("end");
}
}
打印结果:
.
.
.
i=9990
i=9991
i=9992
i=9993
i=9994
i=9995
i=9996
i=9997
i=9998
i=9999
end
由此可见interrupt不会停止线程;
2.interrupted():测试当前线程是否已经中断,执行后将当前标识清除;
package Java_test;
import java.io.File;
public class Test {
public static void main(String[] args) {
try {
Mythread thread = new Mythread();
thread.start();
Thread.sleep(100);
thread.interrupt();
System.out.println("interrupted()=" + Thread.interrupted());
System.out.println("interrupted()=" + Thread.interrupted());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Mythread extends Thread {
int i;
public void run() {
// TODO Auto-generated method stub
System.out.println("begin");
for (i = 0; i < 10000; i++) {
System.out.println("i=" + i);
}
System.out.println("end");
}
}
打印结果:
.
.
.
i=4640
i=4641
i=4642
interrupted()=false
interrupted()=false
i=4643
i=4644
i=4645
i=4646
i=4647
i=4648
i=4649
.
.
.
修改如下:
package Java_test;
import java.io.File;
public class Test {
public static void main(String[] args) {
Thread.currentThread().interrupt();
System.out.println("interrupted()=" + Thread.interrupted());
System.out.println("interrupted()=" + Thread.interrupted());
}
}
打印结果:
interrupted()=true
interrupted()=false
由于我对main线程interrupt(附上停止标记),所以第一个interrupted是ture,接着,第一个interrupted会清楚停止标记,所以第二个interrupted是false。
3.isinterrupted():测试线程thread对象是否已经是中断状态,但是不清除标识;
package Java_test;
import java.io.File;
public class Test {
public static void main(String[] args) {
Thread.currentThread().interrupt();
System.out.println("interrupted()=" + Thread.currentThread().isInterrupted());
System.out.println("interrupted()=" + Thread.currentThread().isInterrupted());
}
}
打印结果:
interrupted()=true
interrupted()=true
1.异常法(推荐使用)
package Java_test;
import java.io.File;
public class Test {
public static void main(String[] args) {
try {
Mythread mt = new Mythread();
mt.start();
Thread.currentThread().sleep(1000);
mt.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Mythread extends Thread {
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
System.out.println("begin");
if (this.interrupted()) {
throw new InterruptedException();
}
System.out.println("running");
System.out.println("end\n");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("停止线程");
}
}
}
打印结果:
.
.
.
begin
running
end
begin
running
end
begin
停止线程
package Java_test;
import java.io.File;
public class Test {
public static void main(String[] args) {
try {
Mythread mt = new Mythread();
mt.start();
Thread.currentThread().sleep(1000);
mt.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Mythread extends Thread {
public void run() {
// TODO Auto-generated method stub
while (true) {
System.out.println("begin");
if (this.interrupted()) {
System.out.println("停止线程");
return;
}
System.out.println("running");
System.out.println("end\n");
}
}
}
begin
running
end
begin
running
end
begin
停止线程
http://blog.csdn.net/x_i_a_o_hei/article/details/52245239
下篇文章:java多线程技术(二)
http://blog.csdn.net/x_i_a_o_hei/article/details/52350765