前言
该篇中主要学习Thread类的常用API,currentThread,isAlive,sleep,interrupt,isInterrupted,stop,suspend,resume,yieId,join,wait,notify,notifyAll,start等方法的使用以及详解。
1:currentThread() 返回正在被调用的线程信息。
public class threadTest { public static void main(String[] args) { System.out.println(Thread.currentThread().getName()); Runnable myRunnable =new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); } } public class MyRunnable implements Runnable{ @Override public void run() { System.out.println("Runnable:"+Thread.currentThread().getName()); } }运行结果:main Runnable:Thread-0 2:isAlive() 判断当前线程是否处于活动状态,返回boolean public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println(Thread.currentThread().getName()+",begin:"+Thread.currentThread().isAlive()); Runnable myRunnable =new MyRunnable(); Thread thread = new Thread(myRunnable); System.out.println(thread.getName()+",begin:"+thread.isAlive()); thread.start(); System.out.println(thread.getName()+",end:"+thread.isAlive()); } } public class MyRunnable implements Runnable{ @Override public void run() { System.out.println("Runnable:"+Thread.currentThread().getName()+",isAlive:"+Thread.currentThread().isAlive()); } }运行结果:main,begin:true Thread-0,begin:false Thread-0,end:true Runnable:Thread-0,isAlive:trueThread-0,end打印true是因为MyRunnable线程还没有执行完毕,main线程就已经执行System.out.println(thread.getName()+",end:"+thread.isAlive());这段代码了 如果把他换成如下代码
public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println(Thread.currentThread().getName()+",begin:"+Thread.currentThread().isAlive()); Runnable myRunnable =new MyRunnable(); Thread thread = new Thread(myRunnable); System.out.println(thread.getName()+",begin:"+thread.isAlive()); thread.start(); Thread.sleep(1000);//睡眠1秒 System.out.println(thread.getName()+",end:"+thread.isAlive()); } }运行结果:main,begin:true Thread-0,begin:false Runnable:Thread-0,isAlive:true Thread-0,end:false3:sleep()当前线程(正在执行的线程)指定的毫秒数内暂停执行
public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println("begin........"); Runnable myRunnable =new MyRunnable(); Thread thread = new Thread(myRunnable); System.out.println(Thread.currentThread().getName()); thread.start(); Thread.currentThread().sleep(1000); System.out.println("end........"); } } public class MyRunnable implements Runnable{ @Override public void run() { System.out.println("Runnable:"+Thread.currentThread().getName()); } }运行结果 begin........ main Runnable:Thread-0 end........4:getId()线程的唯一标识
public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println("begin........"); Runnable myRunnable =new MyRunnable(); Thread myRunnableThread = new Thread(myRunnable); System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getId()); myRunnableThread.start(); MyThread myThread = new MyThread(); myThread.sleep(1000); myThread.run(); Thread.currentThread().sleep(1000); System.out.println("end........"); } } public class MyThread extends Thread{ @Override public void run() { super.run(); System.out.println("MyThread:"+Thread.currentThread().getId()); } } public class MyRunnable implements Runnable{ @Override public void run() { System.out.println("Runnable:"+Thread.currentThread().getId()); } }运行结果 begin........ main:1 Runnable:9 MyThread:1 end........ 5:yiedld()放弃当前cpu资源,将它让给其他任务去占用cpu执行时间,放弃时间不确定,有可能刚刚放弃马上就获取cpu时间片 public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println("begin........"); MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(1000); System.out.println("end........"); } } public class MyThread extends Thread{ @Override public void run() { long beginTime = System.currentTimeMillis(); int count = 0; for(int i=0; i<800000; i++){ count = count + (i + 1); } long endTime = System.currentTimeMillis(); System.out.println("用时:“"+(endTime-beginTime)+"”,毫秒"); } }运行结果:begin........ 用时:“0”,毫秒 end........ public class MyThread extends Thread{ @Override public void run() { long beginTime = System.currentTimeMillis(); int count = 0; for(int i=0; i<800000; i++){ Thread.yield(); count = count + (i + 1); } long endTime = System.currentTimeMillis(); System.out.println("用时:“"+(endTime-beginTime)+"”,毫秒"); } } 运行结果 begin........ 用时:“141”,毫秒 end........ 6:停止线程在java中有3种方式停止正在运行的线程
(1)使用退出标志,使线程正常退出,也就是run方法执行完成后
(2)使用stop()方法强行终止线程,不推荐
(3)使用interrupt()方法中断线程,interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程
使用interrupt()方法中断线程时,先了解如何判断线程状态是否停止,Thread类中提供了两种方法,
(1)this.interrupted():测试当前线程是否已经中断,执行后具有将状态标志置清除为false的功能。
(2)this.isInterrupted():测试线程是否已经中断,不清除状态标志。
public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println("begin........"); MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(1000); //Thread.currentThread().interrupt(); myThread.interrupt(); System.out.println("myThread:"+myThread.interrupted()); System.out.println("myThread:"+myThread.interrupted()); //System.out.println("main:"+Thread.currentThread().interrupted()); //System.out.println("main:"+Thread.currentThread().interrupted()); System.out.println("end........"); } } public class MyThread extends Thread{ @Override public void run() { long beginTime = System.currentTimeMillis(); int count = 0; for(int i=0; i<800000; i++){ Thread.yield(); count = count + (i + 1); } long endTime = System.currentTimeMillis(); System.out.println("用时:“"+(endTime-beginTime)+"”,毫秒"); } }运行结果 begin........ 用时:“157”,毫秒 myThread:false myThread:false end........例2
public class threadTest { public static void main(String[] args) throws InterruptedException { System.out.println("begin........"); MyThread myThread = new MyThread(); myThread.start(); //Thread.sleep(1000); Thread.currentThread().interrupt(); System.out.println("main:"+Thread.currentThread().interrupted()); System.out.println("main:"+Thread.currentThread().interrupted()); System.out.println("end........"); } } public class MyThread extends Thread{ @Override public void run() { long beginTime = System.currentTimeMillis(); int count = 0; for(int i=0; i<800000; i++){ Thread.yield(); count = count + (i + 1); } long endTime = System.currentTimeMillis(); Thread.currentThread().interrupt(); System.out.println("myThread:"+Thread.currentThread().isInterrupted()); System.out.println("myThread:"+Thread.currentThread().isInterrupted()); System.out.println("用时:“"+(endTime-beginTime)+"”,毫秒"); } } 运行结果begin........ main:true main:false end........ myThread:true myThread:true 用时:“129”,毫秒 interrupted()的确判断出当前线程是否停止状态,但是为什么第一个interrupted判断为true第二个interrupted判断为false呢? 如果连续两次调用该方法,则第二次调用将返回false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外) (1)异常法停止线程:
public class threadTest { public static void main(String[] args) { System.out.println("begin........"); try { MyThread myThread = new MyThread(); myThread.start(); myThread.interrupt(); Thread.sleep(5000); Thread.currentThread().interrupt(); if(Thread.currentThread().interrupted()){ System.out.println("main:已经停止状态"); throw new InterruptedException(); } System.out.println("main1:"+Thread.currentThread().isInterrupted()); } catch (InterruptedException e) { System.out.println("main2:"+Thread.currentThread().isInterrupted()); e.printStackTrace(); } System.out.println("end........"); } } public class MyThread extends Thread{ @Override public void run() { try { for(int i=0; i<10; i++){ if(Thread.currentThread().interrupted()){ System.out.println("已经停止状态"); throw new InterruptedException(); } System.out.println("i="+i); } System.out.println("myThread:"+Thread.currentThread().isInterrupted()); } catch (InterruptedException e) { System.out.println("exception"); // TODO Auto-generated catch block e.printStackTrace(); } } } 运行结果: begin........ 已经停止状态 exception java.lang.InterruptedException at com.suicai.util.MyThread.run(MyThread.java:10) main:已经停止状态java.lang.InterruptedException at com.suicai.util.threadTest.main(threadTest.java:16) main2:false end........ (2)沉睡中停止线程 public class threadTest { public static void main(String[] args) { System.out.println("begin........"); try { MyThread myThread = new MyThread(); myThread.start(); myThread.interrupt(); Thread.sleep(5000); Thread.currentThread().interrupt(); if(Thread.currentThread().interrupted()){ System.out.println("main:已经停止状态"); throw new InterruptedException(); } System.out.println("main1:"+Thread.currentThread().isInterrupted()); } catch (InterruptedException e) { System.out.println("main2:"+Thread.currentThread().isInterrupted()); e.printStackTrace(); } System.out.println("end........"); } } public class MyThread extends Thread{ @Override public void run() { try { System.out.println("开始............."); Thread.currentThread().sleep(20000); System.out.println("myThread:"+Thread.currentThread().isInterrupted()); System.out.println("结束............."); } catch (InterruptedException e) { System.out.println("exception"); e.printStackTrace(); } } } 运行结果 begin........ 开始............. exception java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at com.suicai.util.MyThread.run(MyThread.java:8) main:已经停止状态java.lang.InterruptedException at com.suicai.util.threadTest.main(threadTest.java:16) main2:false end........ (3)使用stop()方法暴力停止线程 public class threadTest { public static void main(String[] args) { System.out.println("begin........"); try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(5000); Thread.currentThread().interrupt(); myThread.stop(); if(Thread.currentThread().interrupted()){ System.out.println("main:已经停止状态"); throw new InterruptedException(); } System.out.println("main1:"+Thread.currentThread().isInterrupted()); } catch (InterruptedException e) { System.out.println("main2:"+Thread.currentThread().isInterrupted()); e.printStackTrace(); } System.out.println("end........"); } } public class MyThread extends Thread{ private int i =0; @Override public void run() { try { while (true) { i++; System.out.println("i="+i); Thread.sleep(10000); } } catch (InterruptedException e) { System.out.println("exception"); e.printStackTrace(); } } } 运行结果 begin........ i=1 main:已经停止状态 main2:false java.lang.InterruptedException end........ at com.suicai.util.threadTest.main(threadTest.java:16)