JavaSE笔记7.9-多线程-Thread类的其他方法

1. setDaemon()

在这里插入图片描述

(1)守护线程特点

线程分为两种:用户线程(User Thread)和守护线程(Daemon Thread)。
守护线程特点:开启后,和用户线程共同抢夺CPU的执行权运行(开启和运行都和用户线程无区别);当所有的用户线程都结束后,守护线程会自动结束

(2)未使用setDaemon( )
class StopThread implements Runnable{
    private boolean flag=true;
    public synchronized void run(){
        while (flag){
            try{
                wait();
            }
            catch (InterruptedException e){
                System.out.println(Thread.currentThread().getName()+"...Exception");
            }
            System.out.println(Thread.currentThread().getName()+"...run");
        }
    }
}
class StopThreadDemo{
    public static void main(String[] args){
        StopThread st=new StopThread();
        Thread t1=new Thread(st);
        Thread t2=new Thread(st);
        t1.start();
        t2.start();
        int num=0;
        while (true){
            if(num++==60){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"...run"+num);
        }
        System.out.println("over");
    }
}

运行结果是:
线程t1和t2被冻结,不能终止,运行未结束。
在这里插入图片描述

(3)使用setDaemon( )
class StopThread implements Runnable{
    private boolean flag=true;
    public synchronized void run(){
        while (flag){
            try{
                wait();
            }
            catch (InterruptedException e){
                System.out.println(Thread.currentThread().getName()+"...Exception");
            }
            System.out.println(Thread.currentThread().getName()+"...run");
        }
    }
}
class StopThreadDemo{
    public static void main(String[] args){
        StopThread st=new StopThread();
        Thread t1=new Thread(st);
        Thread t2=new Thread(st);
        //将线程t1和t2标记为守护线程
        t1.setDaemon(true);
        t2.setDaemon(true);

        //将线程设置为守护线程后才启动
        t1.start();
        t2.start();
        int num=0;
        while (true){
            if(num++==60){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"...run"+num);
        }
        System.out.println("over");
    }
}

运行结果是:
线程t1和t2被冻结,但主线程结束后,守护线程t1和t2自动结束,运行结束。
在这里插入图片描述

2. join( )

在这里插入图片描述

(1)join( )定义
t.join();//等待线程t执行完毕
t.join(1000);//等待线程t执行,等待时间是1000ms

t调用join方法,即抢夺CPU的执行权,主线程将CPU的执行权给t,主线程变为冻结状态;
t线程运行至结束,主线程才恢复到运行状态;

(2)未使用join( )
class Demo implements Runnable{
    public void run(){
        for(int i=0;i<70;i++){
            System.out.println(Thread.currentThread().getName()+"....."+i);
        }
    }
}
class JoinDemo{
    public static void  main(String[] args){
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        t2.start();
        //主线程
        for(int i=0;i<80;i++){
            System.out.println("main....."+i);
        }
        System.out.println("over");
    }
}

运行结果是:
Thread0、Thread1、main三者交替运行
在这里插入图片描述

(3)使用join( )
class Demo implements Runnable{
    public void run(){
        for(int i=0;i<70;i++){
            System.out.println(Thread.currentThread().getName()+"....."+i);
        }
    }
}
class JoinDemo{
    public static void  main(String[] args) throws InterruptedException {
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        
        //在t2启动之前,t1调用join()
        t1.join();
        
        t2.start();
        //主线程
        for(int i=0;i<80;i++){
            System.out.println("main....."+i);
        }
        System.out.println("over");
    }
}

运行结果是:
Thread0先运行至结束,Thread1和main才交替运行

(4)在主线程中写join( ),抢夺的是主线程的执行权
class Demo implements Runnable{
    public void run(){
        for(int i=0;i<70;i++){
            System.out.println(Thread.currentThread().getName()+"....."+i);
        }
    }
}
class JoinDemo{
    public static void  main(String[] args) throws InterruptedException {
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        t2.start();
        
        //在t2启动之后,t1调用join()
        t1.join();
        
        //主线程
        for(int i=0;i<80;i++){
            System.out.println("main....."+i);
        }
        System.out.println("over");
    }
}

运行结果是:
Thread0和Thread1交替运行;但主线程需要等到Thread0结束后才能恢复运行状态

(5)特点

当A线程执行到了B线程的join()时,A线程就会等待,等B线程都执行完,A才会执行。
join()可以用来临时加入线程执行。

当B线程挂着了,A线程也挂着了,可以用interrupt来强制唤醒A线程

3. toString( )

在这里插入图片描述

使用toString( )
class Demo implements Runnable{
    public void run(){
        for(int x=0;x<70;x++){
            System.out.println(Thread.currentThread().toString()+"....."+x);
        }
    }
}
class ToStringDemo{
    public static void main(String[] args){
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        t2.start();
    }
}

运行结果是:
Thread0和Thread1的线程优先级为5,属于线程组main
在这里插入图片描述

4. setPriority( )

(1)优先级
  • 优先级代表着抢夺资源的频率
  • 所有线程包括主线程的默认优先级为5
  • 优先级的区间为1~10
  • 优先级只有1、5、10最明显,有专属的字段名称,定义为静态常量值,增强阅读性
    在这里插入图片描述
(2)使用setPriority( )修改优先级

在这里插入图片描述

class Demo implements Runnable{
    public void run(){
        for(int x=0;x<70;x++){
            System.out.println(Thread.currentThread().toString()+"....."+x);
        }
    }
}
class SetPriorityDemo{
    public static void  main(String[] args){
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        //给线程t1设置最高优先级10
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.start();
    }
}

运行结果是:
在这里插入图片描述

5. yeild( )

(1)作用
  • 放弃当前线程获取CPU的执行权,将让其它的线程去获取(但这个是不固定的)
  • 可以稍微减缓线程执行的频率,减少同一个线程连续多次执行的现象,使得线程有平均运行的机会
(2)不使用yield( )
class Demo implements Runnable{
    public void run(){
        long time=System.currentTimeMillis();
        for(int x=0;x<70;x++){
         System.out.println(Thread.currentThread().toString()+"....."+x);
        }
   }
}
class YieldDemo{
    public static void  main(String[] args){
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        t2.start();
    }
}

运行结果是:
Thread0和Thread1没有交替执行,1个线程连续多次执行

(2)使用yield( )
class Demo implements Runnable{
    public void run(){
        long time=System.currentTimeMillis();
        for(int x=0;x<70;x++){
            System.out.println(Thread.currentThread().toString()+"....."+x);
            //调用yield()
            Thread.yield();
        }
    }
}
class YieldDemo{
    public static void  main(String[] args){
        Demo d=new Demo();
        Thread t1=new Thread(d);
        Thread t2=new Thread(d);
        t1.start();
        t2.start();
    }
}

运行结果是:
Thread0和Thread1开始交替(但这个是不固定的)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值