Programmer Log17[多线程2]_19.12.09

3.线程停止的三种方式
3.1 设置标记位,可以使线程正常退出

public class TestDemoThread3{
    public static void main(String[] args) throws InterruptedException {
        MyThread3 myThread3 = new MyThread3();
        Thread thread3 = new Thread(myThread3,"子线程A");
        thread3.start();
        Thread.sleep(3000);
        myThread3.setFlag(false);//主线程休眠了3000ms后继续执行后面的内容,于是flag被设成false,不再进行循环
        System.out.println("代码结束");
    }

}

class  MyThread3 implements Runnable{
    private boolean flag = true;
    public void run(){
        int i =1;
        while(flag){
            try{
                Thread.sleep(500);
                System.out.println("第"+i+"次执⾏,线程名称为:"+Thread.currentThread().getName());
                i++;
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }

        }
        public void setFlag(boolean flag){
        this.flag = flag;
        }
    }

3.2 使用stop()(此方法已经废弃)

MyThread myThread = new MyThread();
Thread thread1 = new Thread(myThread,"⼦线程A");
thread1.start();
Thread.sleep(3000);
thread1.stop();
System.out.println("代码结束");

废弃原因是stop()会解除所有的锁定,可能正在执行到一个方法的一半,这样就会产生不完整的数据。
3.3使用Thread.interrupt()
4.线程优先级
线程优先级越高,越有可能先执行
4.1 设置优先级

class MyThread implements Runnable{
    public void run(){
        for(int i=0;i<5;i++){
            System.out.println("当前线程"+Thread.currentThread().getName()+" i ="+i);
        }
    }
}


public class TestDemo{
    public static void main(String[] args){
        MyThread mt = new MyThread();
        Thread t1 = new Thread(mt,"1");
        Thread t2 = new Thread(mt,"2");
        Thread t3 = new Thread(mt,"3");
        t1.setPriority(Thread.MIN_PRIORITY);//最低优先级 1
        t2.setPriority(Thread.NORM_PRIORITY);//中等优先级 5
        t3.setPriority(Thread.MAX_PRIORITY);//最高优先级 10
        System.out.println(t1.getPriority());        
        t1.start();
        t2.start();
        t3.start();
        
    }
}

4.2 线程的继承性
线程具有继承关系,当A线程中启动B线程时,B和A的优先级一样

class A implements Runnable {
    @Override
    public void run() {
    System.out.println("A的优先级为:" +Thread.currentThread().getPriority());
    Thread thread = new Thread(new B());
    thread.start();
    }
}
class B implements Runnable {
    @Override
    public void run() {
    System.out.println("B的优先级为:" +Thread.currentThread().getPriority());
    }
}
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new A());
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
}

5.守护线程
守护线程在所守护的线程全部停止工作后才会停止工作

public class TestDemoThread5 {
    public static void main(String[] args) throws InterruptedException{
       Thread thread1 = new Thread(new MyThread5(),"子线程A");
       thread1.setDaemon(true);//设为守护线程
       thread1.start();

        Thread thread2 = new Thread(new MyThread5(),"子线程B");
        thread2.start();
        Thread.sleep(3000);
        thread2.interrupt();//中断用户线程
        Thread.sleep(2000);
        System.out.println("代码结束");

    }
}

class MyThread5 implements Runnable{
    private int i=0;
    public void run(){
       try{
           while(true){
               i++;
               System.out.println("线程名称"+Thread.currentThread().getName()+" i="+i+"是否为守护线程"+Thread.currentThread().isDaemon());
               Thread.sleep(1000);
           }
           }catch(InterruptedException e){
               System.out.println("线程名称 "+Thread.currentThread().getName()+"中断线程了");
       }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值