java多线程技术(二)

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
.
.
.


可见,虽然给Mythread一个停止标志,但是Thread.interrupted()的结果都是false,因为这是main线程,main线程没有停止标志。

修改如下:

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
停止线程


2.return法

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
停止线程


上篇文章:java多线程技术(一)

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












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值