线程的状态
状态之间的转换
线程的启动和终止
start
stop(废弃)
interrupt
volatile Boolean stop=true
Thread.interrupted对设置中断标识的线程进行复位<br>设置interrupt=false
案例
案例分析:<br>1、运行案例代码<br>2、jps查看进程号<br>3、jstack 进程号查看进程状态<br>
Thread state案例<br>package cn.com.hiveview.mediaapi.module.test.thread;<br>import java.util.concurrent.TimeUnit;<br>/**<br> * Created by Yan on 2019/1/31.<br> */<br>public class ThreadState {<br> public static void main(String[] args) {<br> new Thread(() -> {<br> while (true) {<br> try {<br> TimeUnit.SECONDS.sleep(100);<br> } catch (Exception e) {<br> }<br> }<br> }, "thread timewaitting").start();<br> new Thread(() -> {<br> while (true) {<br> synchronized (ThreadState.class) {<br> try {<br> ThreadState.class.wait();<br> } catch (Exception e) {<br> }<br> }<br> }<br> }, "waitting ").start();<br> new Thread (new BlockDemo(),"blocked -0").start();<br> new Thread (new BlockDemo(),"blocked -1").start();<br> }<br> static class BlockDemo extends Thread {<br> public void run() {<br> synchronized (BlockDemo.class) {<br> while (true) {<br> try {<br> TimeUnit.SECONDS.sleep(100);<br> } catch (Exception e) {<br> }<br> }<br> }<br> }<br> }<br>} <br>
interrupt案例<br>package cn.com.hiveview.mediaapi.module.test.thread;<br>import java.util.concurrent.TimeUnit;<br>/**<br> * Created by Yan on 2019/2/1.<br> */<br>public class InterruptDemo {<br> static int i = 0;<br> public static void main(String[] args) throws InterruptedException {<br> Thread thread = new Thread(() -> {<br> while (!Thread.currentThread().isInterrupted()) {<br> i++;<br> }<br> System.out.println("i=" + i);<br> }, "interrupt demo");<br> thread.start();<br> TimeUnit.SECONDS.sleep(1);<br> thread.interrupt();<br> }<br>} <br>interrupt会调用os的interrupt。os的interrupt会设置isInterrupted=true
isInterrupted 案例分析<br>package cn.com.hiveview.mediaapi.module.test.thread;<br>import java.util.concurrent.TimeUnit;<br>/**<br> * Created by Yan on 2019/2/1.<br> */<br>public class TheadInterruptedDemo {<br> /*public static void main(String[] args) throws InterruptedException {<br> Thread thread = new Thread(() -> {<br> while (true) {<br> boolean flag = Thread.currentThread().isInterrupted();<br> if (flag) {<br> System.out.println("before interrupt flag="+flag);<br> Thread.currentThread().interrupted();<br> System.out.println("after interrupted flag="+Thread.currentThread().isInterrupted());<br> }<br> }<br> });<br> thread.start();<br> TimeUnit.SECONDS.sleep(1);<br> thread.interrupt();<br> }*/<br> public static void main(String[] args) throws InterruptedException {<br> Thread thread=new Thread(()->{<br> while (true){<br> try {<br> Thread.sleep(100000);<br> } catch (InterruptedException e) {<br> e.printStackTrace();<br> }<br> }<br> });<br> thread.start();<br> TimeUnit.SECONDS.sleep(1);<br> System.out.println("before------ " +thread.isInterrupted());<br> thread.interrupt();<br> System.out.println("before " +thread.isInterrupted());<br> TimeUnit.SECONDS.sleep(1);<br> System.out.println("after "+thread.isInterrupted());<br> }<br>} <br>before------ false<br>before true<br>java.lang.InterruptedException: sleep interrupted<br> at java.lang.Thread.sleep(Native Method)<br> at cn.com.hiveview.mediaapi.module.test.thread.TheadInterruptedDemo.lambda$main$0(TheadInterruptedDemo.java:28)<br> at java.lang.Thread.run(Thread.java:748)<br>after false<br>