Thread类的相关方法

package thread;

public class Test01 extends Thread {
    int count = 0;

    @Override
    public void run() {
        // currentThread(): 返回当前线程
        System.out.println(Thread.currentThread().getName() + "线程运行的代码");
        for (int i = 0; i < 5; i++) {
            count++;
            System.out.println(Thread.currentThread().getName() + "这是逻辑代码" + count);
        }
    }
}

class T {
    public static void main(String[] args) {
        Test01 t = new Test01();
        Test01 t1 = new Test01();

        Thread tt = new Thread(t);
        Thread tt1 = new Thread(t1);
        tt.start();

        // 设置名称
        tt.setName("儿子");
        tt1.setName("儿子1");
        System.out.println("===========================");
        tt1.start();
        System.out.println("=========================");
        
        // 如果在创建线程的时候没有给名称,那么得到的就是系统默认的名称
        System.out.println(tt.getName());
        System.out.println(tt1.getName());

        /**
         * 线程的优先级: 就是哪个线程有较大概率先执行
         * 优先级是用数字1-10表示,数字越大优先级越高,如果没有设置就默认5
         */
        
        // 查看线程优先级
        System.out.println(tt.getPriority());
        System.out.println(tt1.getPriority());
        
        // 设置线程优先级
        tt.setPriority(1);
        tt1.setPriority(10);
        
        System.out.println(tt.getPriority());
        System.out.println(tt1.getPriority());
    }
}

package thread;

public class Demo01 extends Thread {
    int count = 0; // 修正变量名为count,拼写错误

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "线程运行的代码");
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i % 2 == 0) {
                Thread.yield();
            }

            count++;
            System.out.println(Thread.currentThread().getName() + "这是逻辑代码" + count);
        }
    }
}

class B {
    public static void main(String[] args) throws InterruptedException {
        Demo01 demo01 = new Demo01(); // 创建Demo01对象
        Demo01 demo02 = new Demo01(); // 创建另一个Demo01对象

        Thread thread1 = new Thread(demo01); // 使用Demo01对象创建线程
        Thread thread2 = new Thread(demo02); // 使用另一个Demo01对象创建线程

        System.out.println("111111111111111111111111");
        thread1.start(); // 启动线程1
        thread2.start(); // 启动线程2

        System.out.println(thread2.isAlive()); // 判断线程2是否还活着

        System.out.println("22222222222222222222222");

        // thread1.stop(); // 不推荐使用stop()方法强行结束线程(已注释掉)

        System.out.println("333333333333333333333");

        thread1.join(); // 等待线程1执行完毕,再执行其他代码

        System.out.println("4444444444444444444444");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值