• 33.线程中常用的方法【20220811】


    线程中常用的方法

    1.static Thread currentThread()得到当前正在运行的线程对象
    2.void start() 启动线程
    3.String getName()返回该线程的名称。
            1.当没有设置线程名称的时候,系统会赋予线程一个默认的名称“Thread-0,Thread-1......”
            2.主线程【主方法的执行线程】的名称默认是“main”
    4.void    setName(String name)设置线程名称

    1. package com.wangxing.test;
    2. public class MyThread implements Runnable{
    3. @Override
    4. public void run() {
    5. for(int i=1;i<=100;i++){
    6. //1.static Thread currentThread()得到当前正在运行的线程对象
    7. Thread dangqianThread=Thread.currentThread();
    8. //3.String getName()返回该线程的名称。
    9. String threadname=dangqianThread.getName();
    10. System.out.println(threadname+"===="+i);
    11. }
    12. }
    13. }
    14. package com.wangxing.test;
    15. public class TestMain {
    16. public static void main(String[] args) {
    17. //得到主线程的名称
    18. //1.static Thread currentThread()得到当前正在运行的线程对象
    19. //Thread mainth=Thread.currentThread();
    20. //String name=mainth.getName();
    21. //System.out.println("主线程的线程名称=="+name);
    22. //创建目标对象
    23. MyThread mt=new MyThread();
    24. //创建线程对象
    25. //1.通过Thread类的构造方法设置线程名称
    26. //Thread th1=new Thread(mt,"线程1");
    27. //Thread th2=new Thread(mt,"线程2");
    28. Thread th1=new Thread(mt);
    29. Thread th2=new Thread(mt);
    30. //2.void setName(String name)设置线程名称
    31. th1.setName("线程A");
    32. th2.setName("线程B");
    33. //启动线程
    34. //2.void start() 启动线程
    35. th1.start();
    36. th2.start();
    37. }
    38. }

    线程的优先级

    线程的优先级---就是线程的执行先后。
    默认情况下所有线程的优先级都是一样,都是5。
    我们可以通过void    setPriority(int newPriority) 更改线程的优先级。
    1.线程的优先级有10个级别,分别使用整数1~10来表示。数字越大优先级越高。
    2.为了方便操作,java将10个级别有规定成3个级别,分别是最低的优先级,中等优先级,最高的优先级,并且将这3个级别封装成了静态常量    
        static int    MAX_PRIORITY 线程可以具有的最高优先级。10
        static int    MIN_PRIORITY线程可以具有的最低优先级。1
        static int    NORM_PRIORITY分配给线程的默认优先级。5
        int    getPriority() 返回线程的优先级。
    3.设置线程的优先级的时候,数字越大优先级越高,数字越小优先级越低。优先级越高并不代表就一定会优先执行,只是被优先执行的几率增大,因此不要试图通过控制线程的优先级,来保证某一个线程,总是第一个执行。

    1. package com.wangxing.test1;
    2. public class TestMain {
    3. public static void main(String[] args) {
    4. MyThread mt=new MyThread();
    5. Thread th1=new Thread(mt);
    6. Thread th2=new Thread(mt);
    7. th1.setName("线程A");
    8. th2.setName("线程B");
    9. //默认情况下所有线程的优先级都是一样,都是5。
    10. //void setPriority(int newPriority) 更改线程的优先级。
    11. //参数int newPriority---就是线程优先级的级别
    12. //线程的优先级有10个级别,分别使用整数1~10来表示。数字越大优先级越高。
    13. //th1.setPriority(2);
    14. //th2.setPriority(8);
    15. //为了方便操作,java将10个级别有规定成3个级别,
    16. //最低的优先级----static int MIN_PRIORITY=1
    17. //中等优先级-----static int NORM_PRIORITY=5
    18. //最高的优先级----static int MAX_PRIORITY=10
    19. th1.setPriority(Thread.MIN_PRIORITY);
    20. th2.setPriority(Thread.MAX_PRIORITY);
    21. //设置线程的优先级的时候,数字越大优先级越高,数字越小优先级越低。
    22. //优先级越高并不代表就一定会优先执行,只是被优先执行的几率增大
    23. //因此不要试图通过控制线程的优先级,来保证某一个线总是第一个执行。
    24. th1.start();
    25. th2.start();
    26. //默认情况下所有线程的优先级都是一样,都是5。
    27. //int getPriority() 返回线程的优先级。
    28. int thpri1=th1.getPriority();
    29. int thpri2=th2.getPriority();
    30. //得到主线程的优先级
    31. Thread mainThread=Thread.currentThread();
    32. int mainpri=mainThread.getPriority();
    33. System.out.println("线程A的优先级=="+thpri1);
    34. System.out.println("线程B的优先级=="+thpri2);
    35. System.out.println("主线程的优先级=="+mainpri);
    36. }
    37. }

    守护线程的相关操作方法

    用户线程----通常情况之下我们所创建的线程都是普通线程,非守护线程,也叫用户线程。
    守护线程----也叫精灵线程,当所有用户线程都执行完毕以后,自动结束运行的线程就是守护线程.[共死]
    1.boolean    isDaemon() 测试该线程是否为守护线程。
    2.void    setDaemon(boolean on) 将该线程标记为守护线程用户线程。
    特征:当所有用户线程都执行完毕以后,无论守护线程能否可以继续运行,都要立刻停止运行。

    1. package com.wangxing.test2;
    2. public class MyThread implements Runnable{
    3. @Override
    4. public void run() {
    5. for(int i=1;i<=100;i++){
    6. //1.static Thread currentThread()得到当前正在运行的线程对象
    7. Thread dangqianThread=Thread.currentThread();
    8. //3.String getName()返回该线程的名称。
    9. String threadname=dangqianThread.getName();
    10. System.out.println(threadname+"===="+i);
    11. }
    12. }
    13. }
    14. package com.wangxing.test2;
    15. public class TestThread implements Runnable{
    16. @Override
    17. public void run() {
    18. while(true){
    19. Thread dangqianThread=Thread.currentThread();
    20. String threadname=dangqianThread.getName();
    21. System.out.println(threadname+"====守护线程");
    22. }
    23. }
    24. }
    25. package com.wangxing.test2;
    26. public class TestMain {
    27. public static void main(String[] args) {
    28. MyThread mt=new MyThread();
    29. Thread th1=new Thread(mt);
    30. Thread th2=new Thread(mt);
    31. th1.setName("线程A");
    32. th2.setName("线程B");
    33. Thread th3=new Thread(new TestThread());
    34. th3.setName("线程C");
    35. //2.void setDaemon(boolean on) 将该线程标记为守护线程用户线程。
    36. th3.setDaemon(true);
    37. //1.boolean isDaemon() 测试该线程是否为守护线程。
    38. //System.out.println("线程A---isDaemon=="+th1.isDaemon());
    39. //System.out.println("线程B---isDaemon=="+th2.isDaemon());
    40. //System.out.println("线程C---isDaemon=="+th3.isDaemon());
    41. th1.start();
    42. th2.start();
    43. th3.start();
    44. }
    45. }

    static void    sleep(long millis) 设置线程休眠【暂停】指定的时间【毫秒】

    1. 例如:闹钟程序
    2. package com.wangxing.test3;
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;
    5. public class MyThread implements Runnable{
    6. //保存闹钟时间的变量
    7. private String naozhongtime="";
    8. public MyThread(String naozhongtime){
    9. this.naozhongtime=naozhongtime;
    10. }
    11. @Override
    12. public void run() {
    13. //设置时间格式
    14. SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    15. boolean flag=true;
    16. while(flag){
    17. //static void sleep(long millis) 设置线程休眠【暂停】指定的时间【毫秒】
    18. try {
    19. Thread.sleep(1000);
    20. } catch (InterruptedException e) {
    21. e.printStackTrace();
    22. }
    23. //得到当前系统时间
    24. Date date=new Date();
    25. String time=sdf.format(date);
    26. System.out.println(time);
    27. if(time.equals(naozhongtime)){
    28. System.out.println("闹钟时间到!");
    29. flag=false;
    30. }
    31. }
    32. }
    33. }
    34. package com.wangxing.test3;
    35. import java.io.BufferedReader;
    36. import java.io.InputStreamReader;
    37. public class TestMain {
    38. public static void main(String[] args) throws Exception{
    39. System.out.println("请输入闹钟时间:");
    40. BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    41. String naozhongtime=input.readLine();
    42. //创建目标对象
    43. MyThread myth=new MyThread(naozhongtime);
    44. //创建线程对象
    45. Thread th=new Thread(myth);
    46. //启动线程
    47. th.start();
    48. }
    49. }

    void    interrupt() 中断线程休眠【暂停】。会进入异常【InterruptedException】。

    1. package com.wangxing.test4;
    2. import java.text.SimpleDateFormat;
    3. import java.util.Date;
    4. public class MyThread implements Runnable{
    5. @Override
    6. public void run() {
    7. for(int i=1;i<=100;i++){
    8. try {
    9. Thread.sleep(500);
    10. } catch (InterruptedException e) {
    11. e.printStackTrace();
    12. }
    13. Thread dangqianThread=Thread.currentThread();
    14. String threadname=dangqianThread.getName();
    15. System.out.println(threadname+"===="+i);
    16. if(i==10){
    17. System.out.println("休息10秒,接着执行");
    18. try {
    19. Thread.sleep(10000);
    20. } catch (InterruptedException e) {
    21. System.out.println("休眠时间没有结束,强制被主线程中断");
    22. }
    23. }
    24. }
    25. }
    26. }
    27. package com.wangxing.test4;
    28. import java.io.BufferedReader;
    29. import java.io.InputStreamReader;
    30. public class TestMain {
    31. public static void main(String[] args) throws Exception{
    32. //创建目标对象
    33. MyThread myth=new MyThread();
    34. //创建线程对象
    35. Thread th=new Thread(myth);
    36. th.setName("线程A");
    37. //启动线程
    38. th.start();
    39. for(int i=1;i<=100;i++){
    40. try {
    41. Thread.sleep(500);
    42. } catch (InterruptedException e) {
    43. e.printStackTrace();
    44. }
    45. Thread dangqianThread=Thread.currentThread();
    46. String threadname=dangqianThread.getName();
    47. System.out.println(threadname+"===="+i);
    48. if(i==25){
    49. //void interrupt() 中断线程休眠【暂停】。会进入异常【InterruptedException】。
    50. th.interrupt();
    51. }
    52. }
    53. }
    54. }

    void join(long millis)【强制线程执行】等待该线程终止的时间最长为 millis 毫秒。
    注意:强制线程执行时,其他的线程不能与之交替运行。

    1. package com.wangxing.test5;
    2. import java.text.SimpleDateFormat;
    3. import java.util.Date;
    4. public class MyThread implements Runnable{
    5. @Override
    6. public void run() {
    7. for(int i=1;i<=100;i++){
    8. try {
    9. Thread.sleep(500);
    10. } catch (InterruptedException e) {
    11. e.printStackTrace();
    12. }
    13. Thread dangqianThread=Thread.currentThread();
    14. String threadname=dangqianThread.getName();
    15. System.out.println(threadname+"===="+i);
    16. }
    17. }
    18. }
    19. package com.wangxing.test5;
    20. import java.io.BufferedReader;
    21. import java.io.InputStreamReader;
    22. public class TestMain {
    23. public static void main(String[] args) throws Exception{
    24. //创建目标对象
    25. MyThread myth=new MyThread();
    26. //创建线程对象
    27. Thread th=new Thread(myth);
    28. th.setName("线程A");
    29. //启动线程
    30. th.start();
    31. for(int i=1;i<=100;i++){
    32. try {
    33. Thread.sleep(500);
    34. } catch (InterruptedException e) {
    35. e.printStackTrace();
    36. }
    37. Thread dangqianThread=Thread.currentThread();
    38. String threadname=dangqianThread.getName();
    39. System.out.println(threadname+"===="+i);
    40. if(i==20){
    41. //void join(long millis)【强制线程执行】等待该线程终止的时间最长为 millis 毫秒。
    42. th.join(5000);
    43. }
    44. }
    45. }
    46. }
  • 相关阅读:
    【C++】C/C++内存管理
    一种基于差分进化和灰狼算法的混合优化算法-附代码
    STM32(五):STM32指南者-按键控制灯开关实验
    redis的基本数据类型(一)
    策略模式在springboot中的使用
    RabbitMQ
    初阶扫雷(超详解)
    Go语言 并发与通道
    初识MySQL
    AWS中国峰会2024 半日游
  • 原文地址:https://blog.csdn.net/guizhaiteng/article/details/126286853