• 20240425-线程基础-线程的使用(一)


    线程基础-线程的使用(一)

    1.获取当前线程

    package com.ysf;
    
    public class Tst01CurrentThread {
    
        public static void main(String[] args) {
            Thread thread = Thread.currentThread();
            System.out.println(thread);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.设置线程名称

    • 创建线程或线程池时,必须指定线程或线程池的有意义名称,方便出错时回溯
    package com.ysf;
    
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Tst02SetThreadName {
    
        static AtomicInteger count = new AtomicInteger();
    
        public static void main(String[] args) {
            Thread t = new Thread(()->{
                System.out.println(Thread.currentThread().getName());
            },"module-func-" + count.get());
            t.start();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.线程的优先级

    • 线程的优先级是指CPU调度的优先级
    • 线程优先级最大值:10,最小值:1,默认值5
    • 线程优先级并非决定性因素,效果可能并不会很明显
    package com.ysf;
    
    import java.util.concurrent.TimeUnit;
    
    public class Tst03SetThreadPriority {
    
        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new Thread(()->{
                for (int i = 0;i<100;i++){
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "==>" + i);
                }
            },"priority-1");
            Thread t2 = new Thread(()->{
                for (int i = 0;i<100;i++){
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "==>" + i);
                }
            },"priority-2");
            t1.setPriority(10);
            t2.setPriority(1);
            t1.start();
            t2.start();
            TimeUnit.SECONDS.sleep(2L);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4.线程的让步

    • 可以通过Thread的静态方法yield,让当前线程从运行状态变为就绪状态
    • yield方法并不能强制保证CPU不调度自己
    package com.ysf;
    
    import java.util.concurrent.TimeUnit;
    
    public class Tst04SetThreadYield {
    
        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new Thread(()->{
                for (int i = 0;i<100;i++){
                    if (i == 50){
                        Thread.yield();
                    }
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "==>" + i);
                }
            },"yield-1");
            t1.start();
            Thread t2 = new Thread(()->{
                for (int i = 0;i<100;i++){
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "==>" + i);
                }
            },"yield-2");
            t2.start();
    
            TimeUnit.SECONDS.sleep(2L);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    5.线程的休眠

    • 让线程从运行状态变为等待状态
    • sleep()方法会抛出InterruptedException异常,与结束线程相关
    package com.ysf;
    
    public class Tst05ThreadSleep {
    
        public static void main(String[] args) throws InterruptedException {
            System.out.println(System.currentTimeMillis());
            Thread.sleep(1000L);
            System.out.println(System.currentTimeMillis());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    6.线程的强占

    • 对象方法:join()
      • 既然是一个对象方法,那么就一定需要用线程对象去调用
      • 哪个线程调用了t1.join(),哪个线程就被挂起,直到t1执行完毕后恢复执行
    • 对象方法:join(long mill);
      • 被挂起的线程需要等待t1.join(2000L)执行2秒后恢复执行
      • 如果1秒内t1就执行完了,那么被挂起的线程也会恢复执行
    package com.ysf;
    
    public class Tst06ThreadJoin {
    
        public static void main(String[] args) throws InterruptedException {
            Thread t1 = new Thread(()->{
                String name = Thread.currentThread().getName();
                for(int i = 0;i<10;i++){
                   System.out.println(name + "==>" + i);
               }
            },"join-1");
            t1.start();
            for (int i = 0;i<5;i++){
                // 让t1线程join进来,因为是用t1这个线程对象调用,所以一定是在另外一个线程中让t1加入进来
                if (i == 1){
                    t1.join();
                }
                System.out.println(i);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    Redis 如何实现数据不丢失的?
    uniapp 在线升级(热更新)及java后台
    Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
    C语言--每日五道练习题--Day17
    PMP最新考纲难在哪里?面对新教材的来袭,我该怎么计划考试?
    Python GUI_Tinkter学习笔记
    PROTEUS可以在单片机设计时帮助你做什么
    Profinet总线模拟输出模块
    FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
    滴滴SQL面试题之打车业务问题如何分析
  • 原文地址:https://blog.csdn.net/YSF2017_3/article/details/138198993