取得当前JVM中正在执行的线程对象
public static native Thread currentThread();静态,通过类名获得
1、线程名称的命名和取得
命名:
构造方法:public Thread(Runnable target,String name)
set方法:public final synchronized void setName(String name)
取得:
public final String getName()
2、线程休眠:sleep(long millis)
让线程暂缓执行以下,等到预计之间之后再执行,会让当前线程立即交出cpu,但不会释放对象锁
public static native void sleep(long millis) throws InterruptedException;
public static void sleep(long millis, int nanos) throws InterruptedException
3、线程让步:yield()
暂停当前正在执行的线程对象,并执行其他线程(只能让拥有相同优先级的线程获取CPU资源)。
当前线程不会立即交出CPU,交出时间由系统调度。不会释放对象锁
与sleep的区别是,不能控制交出的具体时间,交出的时间由系统决定。并不是让线程到阻塞队列,而是回到就绪状态,重新获取CPU时间片段。
public static native void yield();
4、线程等待:join()对象方法,而不是本地方法
在A线程中调用B线程类.join()方法,A线程就会等B线程执行完成之后接着执行。就是让A等待B,线程A需要等待线程B执行完毕之后解除阻塞,再回到就绪状态
eg:这时主线程(主方法所在的线程)就会等待thread1执行完毕之后,再执行,也就是说finsh在最后打印。
public class JoinThread {
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
Runnable runnable = new Join();
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
Thread thread3 = new Thread(runnable);
thread1.start();
thread2.start();
thread3.start();
thread1.join();
System.out.println("finsh");
}
}
class Join implements Runnable{
@Override
public void run() {
for(int i = 0;i<10;i++){
System.out.println(Thread.currentThread().getName()+",i="+i);
}
}
}
join()内部调用的是public final native void wait(long timeout) throws InterruptedException;join是对wait的包装,wait是Object中的方法,他是被native修饰的本地方法。
public final void join() throws InterruptedException{join(0);}
public final synchronized void join(long millis) throws InterruptedException
public final synchronized void join(long millis,int nanos) throws InterruptedException
5、线程停止的三种方法
a、设置标志位
public class StopThread {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = new Stop();
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(1000);
((Stop) runnable).setFlag(false);
}
}
class Stop implements Runnable{
int i = 0;
Boolean flag = true;
public void setFlag(Boolean b){
this.flag = b;
}
@Override
public void run() {
while(flag){
System.out.println(Thread.currentThread().getName()+" i="+i++);
}
System.out.println("线程停止");
}
}
"线程停止"会打印出来。
b、调用Thread类提供的stop方法强行关闭线程 @Deprecated-----》表示过期的注解public final void stop()
本方法已经不推荐使用
public class StopThread {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = new Stop();
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(1000);
thread.stop();
}
}
class Stop implements Runnable{
int i = 0;
Boolean flag = true;
public void setFlag(Boolean b){
this.flag = b;
}
@Override
public void run() {
while(flag){
System.out.println(Thread.currentThread().getName()+" i="+i++);
}
System.out.println("线程停止");
}
}
此时不会将"线程停止"会打印出来,所以stop()方法是立即退出
c、调用Thread类提供的interrupt(): public void interrupt()
1若线程中没有使用类似sleep()/wait()/join()时(没有将线程阻塞),调用此线程对象的interrupt方法,并不会真正中断线程,只是简单地将线程的状态置为interrupt而已,我们可以根据此状态来进一步确定如何处理线程。
调用Thread类提供的public boolean isInterrupted()可以检测当前线程的状态是否为中断状态
public class StopThread {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = new Stop();
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(200);
//thread.stop(); //stop()方法使之停止
thread.interrupt();
}
}
class Stop implements Runnable{
int i = 0;
Boolean flag = true;
public void setFlag(Boolean b){
this.flag = b;
}
@Override
public void run() {
while(flag) {
System.out.println(Thread.currentThread().getName() +
" i=" + i++ + " " + Thread.currentThread().isInterrupted());
}
System.out.println("线程停止");
}
}
运行结果:没有将线程阻塞,调用此线程对象的interrupt方法,并不会真正中断线程,只是简单地将线程的状态置为interrupt而已
2若线程中调用了阻塞线程的方法如:xsleep()/wait()/join()方法,此时在调用interrupt()方法会抛出异常InterruptedException(中断异常),同时将线程状态还原,就是将isInterrupted=false
设置标志位:在线程阻塞的时候不能立即停下来,必须等这次阻塞结束,再次调度时才能发现标志位改变了
而interrupt()方法在线程阻塞时会抛出异常,可以在catch块中停止线程
public class StopThread {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = new Stop();
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
class Stop implements Runnable{
int i = 0;
Boolean flag = true;
public void setFlag(Boolean b){
this.flag = b;
}
@Override
public void run() {
while(flag){
try {
Thread.sleep(2);
System.out.println(Thread.currentThread().getName()+
" i="+i++ +" "+Thread.currentThread().isInterrupted()+" 在try中");
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+
" i="+i++ +" "+Thread.currentThread().isInterrupted()+" 在catch中");
e.printStackTrace();
}
}
System.out.println("线程停止");
}
}
运行结果