1.线程停止2.线程休眠3.线程礼让4.Join5.观测线程状态6.线程的优先级7.守护线程
多线程的状态:
1.线程停止 建议线程正常停止—>利用次数,不建议死循环建议使用标志位—>设置一个标志位不要使用stop或者destroy等过时,或者JDK不建议使用的方法
自定义stop,可以看到threa停止,但是mian函数还在跑
package statue;public class TestStop implements Runnable{ //设置一个标识位 boolean flag=true; @Override public void run() { int i=0; while (flag){ System.out.println("run Thread"+ i++); } } //2设置一个公开的方法,停止线程,转换标志 public void stop(){ this.flag=false; } public static void main(String[] args) { TestStop testStop=new TestStop(); new Thread(testStop).start(); for (int i = 0; i < 1000; i++) { System.out.println("main"+i); if (i==900){ testStop.stop(); System.out.println("run停止了!"); } } }}
2.线程休眠sleep可以模拟网络延时,放大问题的发生性。应用例子:多线程卖票,倒计时,获取系统时间。每一个对象都有一个锁,sleep不会释放锁。
package statue;import java.text.SimpleDateFormat;import java.util.Date;public class TestSleep { public static void main(String[] args) throws InterruptedException { try { tenDown();//倒计时 } catch (InterruptedException e) { e.printStackTrace(); } //打印当前系统时间 Date startTime= new Date(System.currentTimeMillis()); while (true) { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:MM:SS").format(startTime)); startTime=new Date(System.currentTimeMillis()); //更新时间 } } public static void tenDown() throws InterruptedException { int num=3; while (true) { Thread.sleep(1000); System.out.println(num--); if (num<=0){ break; } } }}
结果
32116:02:50816:02:529
3.线程礼让礼让线程,让当前正在执行的线程暂停,但是不阻塞将程序从运行状态变为就绪状态cpu重新调度,但是礼让不一定成功,看cpu的心情礼让,同时竞争。例如当前A正在运行,B就绪。礼让后,A退出运行状态,此时A和B同时竞争资源。若A运行,则礼让不成功。
package statue;public class TestYield { public static void main(String[] args) { MyYield myYield=new MyYield(); new Thread(myYield,"a").start(); new Thread(myYield,"b").start(); }}class MyYield implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"线程开始执行"); Thread.yield();//礼让 System.out.println(Thread.currentThread().getName()+"线程停止"); }}
4.JoinJoin合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞可以想象插队
package statue;public class TestJoin implements Runnable{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("线程vip来了"+i); } } public static void main(String[] args) throws InterruptedException { //启动我们的线程 TestJoin testJoin=new TestJoin(); Thread thread =new Thread(testJoin); thread.start(); //主线程 for (int i = 0; i < 60; i++) { if (i==20){ thread.join();//插队 } System.out.println("main"+i); } }}
5.观测线程状态 使用:Thread.State state = thread.getState();
public class TestState { public static void main(String[] args) { Thread thread = new Thread(() -> { for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("///"); }); //观察状态 Thread.State state = thread.getState(); //NEW System.out.println(state); //观察启动后 //启动线程 thread.start(); state = thread.getState(); System.out.println(state); //只要线程不终止,就一直输出状态 while (state != Thread.State.TERMINATED) { try { Thread.sleep(100); //更新线程状态 state = thread.getState(); System.out.println(state); } catch (InterruptedException e) { e.printStackTrace(); } } }}
6.线程的优先级getPriority() setPriority(int xxx)
先设置优先级 再启动main 默认优先级5优先级 1-10
package statue;public class TestPriority { public static void main(String[] args) { //主线程输出 System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); MyPriority myPriority = new MyPriority(); Thread t1 = new Thread(myPriority ); Thread t2 = new Thread(myPriority ); Thread t3 = new Thread(myPriority ); Thread t4 = new Thread(myPriority ); Thread t5 = new Thread(myPriority ); Thread t6 = new Thread(myPriority ); t1.start(); //先设置优先级 再启动 t2.setPriority(3); t2.start(); t3.setPriority(5); t3.start(); t4.setPriority(7); t4.start(); t5.setPriority(Thread.MIN_PRIORITY); t5.start(); t6.setPriority(Thread.MAX_PRIORITY); t6.start(); }}class MyPriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority()); }}
可以看出不是优先级高的就会先执行,线程优先级低只是意味着获得调度的概率低,并不是优先级低就不会被调用了,看CPU调度
main-->5Thread-0--->5Thread-4--->1Thread-5--->10Thread-3--->7Thread-1--->3Thread-2--->5
7.守护线程 线程分为用户线程和守护线程虚拟机必须确保用户线程执行完毕虚拟机不用等待守护线程执行完毕如,后台记录操作日志,监控内存,垃圾回收等待。。。守护线程在用户线程执行完毕之后也会执行完毕,不过JVM需要一点时间才停止thread.setDaemon(true);//默认为flase 为用户线程, true为守护线程
上帝守护着你
package statue;public class TestDaemon { public static void main(String[] args) { God god = new God(); You you=new You(); Thread thread = new Thread(god); thread.setDaemon(true);//默认为flase 为用户线程, true为守护线程 thread.start(); new Thread(you).start(); }}class God implements Runnable{ @Override public void run() { while (true){ System.out.println("上帝守护着你"); } }}class You implements Runnable{ @Override public void run() { for (int i = 0; i <36500 ; i++) { System.out.println("开心着活着"); } System.out.println("----goodbye!------"); }}