本文主要是自己学习多线程时,所做的笔记,参考的是b站狂神说java多线程视频讲解。
文章目录- 线程方法
- 1.停止线程 (标志位)
- 2.线程休眠 sleep()
- 3.线程礼让 yield
- 4.线程强制执行 join
- 5.观测线程状态
- 6.线程优先级
- 7.守护线程
不推荐使用JDK提供的stop(),destroy()方法。
建议使用一个标志位进行终止变量,当flag=false,则终止线程运行。
具体代码如下:
public class TestStop implements Runnable{ //1.线程中定义线程体使用的标识 private boolean flag=true; @Override public void run() { int i=0; //2.线程体使用该标识 while(flag){ System.out.println("Thread ----- run"+i++); } } //3.设置一个公开的方法停止线程,转换标志位 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 < 100; i++) { System.out.println("main"+i); if(i==90){ //调用stop方法切换标志位,让线程停止 testStop.stop(); System.out.println("线程该停止了"+i); } } } }2.线程休眠 sleep()
sleep(时间) 指定当前线程阻塞的毫秒数;
sleep存在异常InterruptedException;
sleep时间到达后线程进入就绪状态;
sleep可以模拟网络延时,倒计时等;
每一个对象都有一个锁,sleep不会释放锁;
以倒计时为例
public class TestSleep { public static void main(String[] args) { try { tenDown(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void tenDown() throws InterruptedException { int num=10; while(true){ Thread.sleep(1000); System.out.println(num--); if(num<=0){ break; } } } }3.线程礼让 yield
礼让线程,让当前正在执行的线程暂停,但不阻塞。
比如就绪礼让线程A和线程B同时争夺cpu时,当A拿到了cpu资源,执行礼让yield(),则放弃cpu资源,与线程B重新竞争。因此礼让不一定会成功。
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()+"线程开始执行"); if(Thread.currentThread().getName().equals("a")){ Thread.yield(); } System.out.println(Thread.currentThread().getName()+"线程停止执行"); } }
礼让成功 礼让不成功
4.线程强制执行 joinJoin合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞;
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<10;i++){ if(i==4){ thread.join(); //插队 } System.out.println("main"+i); } } }5.观测线程状态
public class TestState { public static void main(String[] args) throws InterruptedException { 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(); System.out.println(state); //观察启动后 thread.start(); state=thread.getState(); System.out.println(state); while(state !=Thread.State.TERMINATED){ //只要线程不终止,就一直输出状态 Thread.sleep(100); state=thread.getState();//更新线程状态 System.out.println(state); //输出状态 } } }6.线程优先级
线程优先级用数字表示,范围1~10
Thread.MIN_PRIORITY=1;
Thread.MAX_PRIORITY=10;
Thread.NORM_PRIORITY=5;
使用一下方式改变或获取优先级
getPriority(); setPriority(int xxx);
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); //先设置优先级,再启动 t1.setPriority(1); t1.start(); t2.setPriority(5); t2.start(); t3.setPriority(Thread.MAX_PRIORITY); t3.start(); } } class MyPriority implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority()); } };7.守护线程
线程分为用户线程和守护线程
虚拟机必须确保用户线程执行完毕
虚拟机不用等待守护线程执行完毕,如后台几乎 *** 作日志,监控内存,垃圾回收等
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); //默认为false表示用户线程,正常的线程都是用户线程 thread.start(); //守护线程启动 new Thread(you).start(); //用户线程启动 } } class God implements Runnable{ public void run(){ while (true){ System.out.println("我是守护线程"); } } } class You implements Runnable{ public void run(){ for (int i = 0; i < 10; i++) { System.out.println("我是用户线程"); } System.out.println("========= goodbye! =========="); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)