【Java】线程常用方法的使用及方法作用演示
目录
- 1.线程中断
- 2.线程插队
-
- 1.插队前
- 2.加入t2.join()线程插队后
- 3.守护线程
1.setName //设置线程名称,使之与setName的参数相同。
2.getName //返回线程名称,与setName的参数相同。
3.start //使该线程开始执行,Java虚拟机底层调用该线程的start0方法。
4.run //调用线程对象run方法。
5.setPriority //更改线程的优先级
6.getPriority //获取线程的优先级、
7.sleep //在指定参数的毫秒数让当前正在执行的线程休眠、
8.interrup //中断线程
1.线程中断
8.interrup //中断线程
//当该线程执行到一个interrupt 方法时,就会catch 一个 异常, 可以加入自己的业务代码
//InterruptedException 是捕获到一个中断异常.
package com.method;/** * @version 1.0 * @auther Demo龙 * 1.setName //设置线程名称,使之与setName的参数相同。 * > 2.getName //返回线程名称,与setName的参数相同。 * > 3.start //使该线程开始执行,Java虚拟机底层调用该线程的start0方法。 * > 4.run //调用线程对象run方法。 * > 5.setPriority //更改线程的优先级 * > 6.getPriority //获取线程的优先级、 * > 7.sleep //在指定参数的毫秒数让当前正在执行的线程休眠、 * > 8.interrup //中断线程 */ class Method01 { public static void main(String[] args) throws InterruptedException { //测试相关的方法 T t = new T(); t.setName("DEMO龙"); t.setPriority(Thread.MAX_PRIORITY);//10 t.start();//启动子线程 //主线程打印5个hi ,然后我就中断 子线程的休眠 for(int i = 0; i < 5; i++) { Thread.sleep(1000); System.out.println("hi " + i); } System.out.println(t.getName() + " 线程的优先级 =" + t.getPriority());//1 t.interrupt();//当执行到这里,就会中断 t线程的休眠. }}class T extends Thread { //自定义的线程类 @Override public void run() { while (true) { for (int i = 0; i < 10; i++) { //Thread.currentThread().getName() 获取当前线程的名称 System.out.println(Thread.currentThread().getName() + " Demo龙" + i); } try { System.out.println(Thread.currentThread().getName() + " 休眠中~~~"); Thread.sleep(20000);//20秒 } catch (InterruptedException e) { //当该线程执行到一个interrupt 方法时,就会catch 一个 异常, 可以加入自己的业务代码 //InterruptedException 是捕获到一个中断异常. System.out.println(Thread.currentThread().getName() + "被 interrupt了"); } } }}
演示结果
2.线程插队
1.yield //线程的礼让,让出cpu,让其他线程执行,但礼让的时间不确定,所以一定礼让成功。
2.join //线程的插队。插队的线程一旦插队成功,则肯定先执行完插入的线程的所有的任务。
3.案例:创建一个子线程,每隔一秒输出Hi,20次,主线程每隔一秒输出hello,20次,5次后,让子线程运行完毕后,再运行主线程。
1.插队前
package com.method;/** * @version 1.0 * @auther Demo龙 */public class method02 { public static void main(String[] args) { int times=0; T2 t2 = new T2(); t2.start(); for (int i = 0; i < 20; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("hello"+(++times)+"次"); } }}class T2 extends Thread{ int times=0; @Override public void run() { for (int i = 0; i < 20; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("hi"+(++times)+"次"); } }}
演示结果
2.加入t2.join()线程插队后
/** * @version 1.0 * @auther Demo龙 * > 1.yield //线程的礼让,让出cpu,让其他线程执行,但礼让的时间不确定,所以一定礼让成功。 * > 2.join //线程的插队。插队的线程一旦插队成功,则肯定先执行完插入的线程的所有的任务。 */ public class method02 { public static void main(String[] args) throws InterruptedException{ int times=0; T2 t2 = new T2(); t2.start(); for (int i = 0; i < 20; i++) { Thread.sleep(500); System.out.println("hello"+(++times)+"次"); if(i==4){ t2.join(); } } } } class T2 extends Thread{ int times=0; @Override public void run() { for (int i = 0; i < 20; i++) { try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("hi"+(++times)+"次"); } } }
演示结果
3.守护线程
1.用户线程:也叫工作线程,当线程的任务执行完成或通知方式结束
2.守护线程:一般是为工作线程服务的,当所有的用户线程结束,守护线程自动结束
3.常见的守护线程:垃圾回收机制。
//设置守护线程,当主线程结束时,t3线程也结束
t.setDaemon(true);
package com.method;/** * @version 1.0 * @auther Demo龙 */public class method03 { public static void main(String[] args) { int times=0; T3 t3 = new T3(); //设置守护线程,当主线程结束时,t3线程也结束 t3.setDaemon(true); t3.start(); for (int i = 1; i <= 10; i++) { try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("主线程hello"+(++times)+"次"); if(i==10){ System.out.println("主线程结束,子线程也随之结束"); } } }}class T3 extends Thread{ int times=0; @Override public void run() { for (int i = 0; i < 50; i++) { try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println("子线程hi"+(++times)+"次"); } }}
演示结果