美文网首页
线程常用方法

线程常用方法

作者: 让你变好的过程从来都不会很舒服 | 来源:发表于2021-08-01 19:28 被阅读0次


    注意:interrupt可以终止休眠中的线程
    方法演示:
    public class ThreadMethod {
    
        public static void main(String[] args) throws InterruptedException {
            Thread2 thread2 = new Thread2();
            thread2.setName("幻🦅"); // 修改线程名称
            thread2.setPriority(10); // 设置线程优先级
            thread2.start(); // 启动线程
    
            thread2.getPriority(); // 获得线程优先级
    
            for(int i = 0;i<5;i++){
                Thread.sleep(1000);
            }
            thread2.interrupt(); // 中断线程
        }
    }
    
    class Thread2 extends Thread{
    
        @Override
        public void run() {
    
            while(true){
                for (int i=0;i<100;i++){
                    System.out.println("线程名称:"+Thread.currentThread().getName()+"正在开始运行 :"+i);
                }
    
                try {
                    System.out.println("线程名称:"+Thread.currentThread().getName()+"正在休眠 ");
                    Thread.sleep(200000);
                } catch (InterruptedException e) {
                    System.out.println("线程名称:"+Thread.currentThread().getName()+"已被唤醒 ");
                }
            }
        }
    }
    

    1、yieid和join的区别:是根据cup内核态的紧张程度决定的、yieid不一定会礼让成功,根据cup紧张程度决定、cup自认为不紧张,则可能不成功、但是join是线程加入,只有子线程执行完后,主线程再去执行剩下的代码
    join线程加入demo
    public class ThreadMethod2 {
    
        public static void main(String[] args) throws InterruptedException {
            T2 t2 = new T2();
            t2.start();
            for(int i=0;i<20;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("主线程吃了"+i+"个包子");
                if(i==5){
                    System.out.println("主线程让子线程先吃");
                    t2.join(); //这里相当于让t2线程吃完,主线程再吃
                    System.out.println("子线程吃完了,主线程再吃");
                }
            }
        }
    
    }
    
    class T2 extends Thread{
        @Override
        public void run() {
    
            for(int i =0 ;i<20;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程吃了"+i+"个包子");
            }
    
        }
    }
    


    守护线程
    守护线程是指:当主线程执行完毕后,守护线程就会结束执行,也就是生命周期跟随主线程
    设置方式:
    t2.setDaemon(true); // 设置该线程为守护线程
    

    Demo

    public class ThreadMethod2 {
    
        public static void main(String[] args) throws InterruptedException {
            T2 t2 = new T2();
            t2.setDaemon(true); // 设置该线程为守护线程
            t2.start();
            for(int i=0;i<8;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("主线程吃了"+i+"个包子");
            }
        }
    }
    class T2 extends Thread{
        @Override
        public void run() {
    
            for(;;){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程吃包子");
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:线程常用方法

          本文链接:https://www.haomeiwen.com/subject/novimltx.html