多线程02:线程的常见方法

一、线程的sleep方法

  • 这个方法的目的就是让线程休眠,并且这个操作其实是没有释放锁的
  • 当我们设置sleep时,等于告诉cpu,当前的线程不再运行,持有当前对象的锁。那么这个时候cpu就会切换到另外的线程了。这种操作有些时候是非常好的

1、代码演示(线程每秒输出当前时间,线程每一秒休眠一次)

  • 测试
public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        //获取系统的当前时间
        Date date = new Date(System.currentTimeMillis());
        while (true){
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
            //更新当前时间
            date=new Date(System.currentTimeMillis());
        }
    }
}
  • 运行结果
    在这里插入图片描述

二、使用标志位使得线程停止运行

  • 创建一个StopThread类实现Runnable接口
//建议线程正常停止,不建议死循环
//建议使用标志位
//不要使用stop和destroy等过时的方法
public class StopThread implements Runnable {
    //1.设置一个标志位
    private boolean flag=true;

    public void run() {
        int i=0;
        while (flag){
            System.out.println("Thread-----"+i++);

        }
    }

    //2.设置一个公开的方法停止线程,转换标志位
    public void stop(){
        this.flag=false;
}
}
  • 测试
public class StopTest {
    public static void main(String[] args) {
        StopThread stopThread = new StopThread();
        new Thread(stopThread).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("主线程"+i);
            if (i==888){
                //调用stop方法切换标志位,让线停止
                stopThread.stop();
                System.out.println("该线程已经停止!!!");
            }
        }
    }
}
  • 运行结果
    在这里插入图片描述

三、线程的优先级

  • 创建一个MyPriority类实现Runnable接口
//测试线程的优先级
class MyPriority implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName()+"==========>"+Thread.currentThread().getPriority());
    }
}
  • 测试
public class PriorityTest {
    public static void main(String[] args) {
        //主线程默认的优先级
        System.out.println(Thread.currentThread().getName()+"主线程=====>"+Thread.currentThread().getPriority());
        MyPriority myPriority = new MyPriority();
        Thread t1 = new Thread(myPriority);
        Thread t2 = new Thread(myPriority);
        Thread t3 = new Thread(myPriority);
        Thread t4 = new Thread(myPriority);
        Thread t5 = new Thread(myPriority);

        t1.setPriority(1);
        t1.start();

        t2.setPriority(3);
        t2.start();

        t3.setPriority(5);
        t3.start();

        t4.setPriority(7);
        t4.start();

        t5.setPriority(10);
        t5.start();
    }

}

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

四、线程礼让

  • 创建一个MyYield类实现Runnable接口
//礼让不一定成功
class MyYield implements Runnable{
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始执行");
        //线程礼让
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"线程停止执行");

    }
}
  • 测试
public class YieldTest {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        Thread t1 = new Thread(myYield,"a");
        Thread t2 = new Thread(myYield,"b");
        t1.start();
        t2.start();
    }
}
  • 运行结果
    在这里插入图片描述

五、线程插队

  • 创建一个JoinThread类实现Runnable接口
public class JoinThread implements Runnable {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("线程vip来了"+i);
        }
    }
}

  • 测试
public class JoinTest {
    public static void main(String[] args) throws InterruptedException {
        JoinThread joinThread = new JoinThread();
        Thread thread = new Thread(joinThread);
        thread.start();
        //主线程
        for (int i = 0; i < 100; i++) {
            if (i==20){
                //插队
                thread.join();
            }
            System.out.println("主线程"+i);
        }
    }
}
  • 运行结果
    在这里插入图片描述

六、守护线程

  • 创建一个MyGog类实现Runnable接口
class MyGog implements Runnable{
    public void run() {
        while (true){
            System.out.println("保佑你");
        }
    }
}
  • 创建一个MyDaemon类实现Runnable接口
class MyDaemon implements Runnable{
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("开心每一天");
        }
        System.out.println("再见");
    }
}
  • 测试
public class DaemonTest {
    public static void main(String[] args) {

        MyGog myGog = new MyGog();
        MyDaemon myDaemon = new MyDaemon();


        Thread thread = new Thread(myGog);
        //默认为falae,为用户线程,正常的都为用户线程
        thread.setDaemon(true);
        //启动守护线程
        thread.start();
        //用户线程启动
        new Thread(myDaemon).start();


    }
}
  • 运行结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑AJJD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值