多线程(二)

多线程中的常用方法

  1. id, name和线程对象
//获取线程中的id和name,线程对象
public class IdName implements  Runnable{
    @Override
    public void run() {
       // Thread.currentThread();//获取当前运行的代码在哪一个线程中
        //获取当前线程的id
        System.out.println("当前线程的id:"+Thread.currentThread().getId());
        //获取当前线程的id
        System.out.println("当前线程的name:"+Thread.currentThread().getName());
        for (int i = 0; i <1000 ; i++) { 
                 System.out.println(Thread.currentThread().getName()+"输出 i="+i);
        }
    }
}

测试类

public class Test03 {
    public static void main(String[] args) {
        Runnable runnable = new IdName();
        //创建线程
        Thread thread = new Thread();
        //设置线程的名字
        thread.setName("自定义线程-1");
        //启动线程
        thread.start();
        //当前main方法,执行的是主线程
        System.out.println("当前主线程-----name:"+Thread.currentThread().getName());
        System.out.println("当前主线程-----id"+Thread.currentThread().getId());
        for (int j = 0; j < 1000; j++) {           System.out.println(Thread.currentThread().getName()+"输出 j="+j);
        }
    }
}
  1. sleep(long millitime) 作用:让当前线程"睡眠",执行的millitime毫秒,在指定的millitime毫秒的时间类,当前的线程是阻塞的
public class Test04 {
    public static void main(String[] args) throws InterruptedException {
        //创建匿名内部类 其实和MyRunnable效果一样
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                 //代码每执行一次睡1秒;模拟火箭发射倒计时
                for (int i = 10; i>=0; i--) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //睡眠完成之后接着执行
                    System.out.println("自定义线程Thread倒计时" + i+"秒");
                }
            }
        };
        Thread thread = new Thread(runnable);
        //启动线程
        thread.start();
        //主线程睡眠
        System.out.println("主线程开始睡眠5s");
        Thread.sleep(5000);
        System.out.println("主线程结束睡眠");
    }
}
  1. join() 作用:将其他线程加入到当前线程中,当前线程等待加入的线程执行完毕。比如:此时线程a就进入阻塞状态,知道线程b完全执行结束之后线程a才会结束阻塞状态。
public class Test05 {
    public static void main(String[] args) throws InterruptedException {
        Thread  thread = new Thread(new Runnable() {
            @Override
            public void run() {

                //自定义线程执行For循环
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName()+"输出 i="+i);
                }
            }
        });
        thread.setName("自定义线程-1");
        thread.start();

        //主线程for循环
        for (int j = 0; j < 1000; j++) {
            if (j==100){
                //只要运行到100,调用thread.join()方法
                thread.join();
            }
            System.out.println(Thread.currentThread().getName()+"输出 j="+j);
        }
    }
}

  1. yield() 作用:释放当前CPU的执行权
public class Test06 {
    public static void main(String[] args) {
        Thread thread  = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName()+"输出 i="+i);
                }
            }
        });
        thread.start();

        //主线程for 循环1000次
        for (int j = 0; j < 1000; j++) {
            if (j%2==0){
                //如果j是偶数,就让出当前线程的CPU分片,让出CPU资源为其他线程服务
                //让当前主线程放出CPU资源------>main线程最少执行一定的频率
                // Thread.yield();严重的依赖系统配置,本质是让出CPU资源,但是基本上达不到我们的预期,很少使用
                Thread.yield();
            }
            System.out.println(Thread.currentThread().getName()+"输出 j="+j);
        }
    }
}
  1. setDemon() 作用:设置线程是否是前台线程

①前台线程:main线程,默认创建的自定义线程,都是前台线程,只有所有的线程执行完毕之后,线程才会结束
②后台线程:后台线程就是后台运行,依附于前台线程,只要前台线程结束,后台线程立即终止(不会因为run()方法没有结束而继续执行)

public class Test07 {
//测试后台线程
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName()+"输出 i ="+i);
                }
            }
        });
        //thread默认情况下线程是前台线程===只有该线程对应任务Run必须执行完毕 线程才会结束
        //前台所有线程结束,进程才会结束
        thread.setName("自定义线程-1");
        //设置当前线程为Thread为后台线程,只要前台线程(创建线程main)结束,
        //后台线程立即终止(不会关心线程中的任务是否会执行完毕)
        thread.setDaemon(true);
        thread.start();

        //主线程for 循环
        for (int j = 0; j < 10; j++) {
            Thread.sleep(1);
            System.out.println(Thread.currentThread().getName() + " 输出j = " + j);
        }

        System.out.println("main 主线程(前台线程)代码结束");
    }
}

6.stop() 作用:强制结束线程的生命周期,不推荐使用[已经过时]
7.isAlive()判断当前线程是否存活

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值