第四课 线程基本信息

方法功能
isAlive()判断线程是否还“活”着,即线程是否还未终止
getPriority()获得线程的优先级数值
setPriority(int num)设置线程的优先级数值
setName(String name)给线程定义名称
getName()取得线程名称
currentThread()取得当前正在运行的线程对象
public class ThreadInfoTest {
	public static void main(String[] args) {
		testInfo test01 = new testInfo();
		testInfo test02 = new testInfo();
		Thread thread01 = new Thread(test01);
		thread01.setName("thread01"); // 设置线程名称
		Thread thread02 = new Thread(test02);
		thread02.setName("thread02");

		// Thread中的常量
		// MIN_PRIORITY : 1 --> 最低优先级
		// NORM_PRIORITY : 5 --> 默认优先级
		// MAX_PRIORITY : 10 --> 最高优先级
		thread01.setPriority(Thread.MIN_PRIORITY); // 设置线程优先级
		thread02.setPriority(Thread.MAX_PRIORITY); // 设置优先级后不代表线程的执行顺序,只表示线程被执行的概率高低
		thread01.start();
		thread02.start();
		
		try {
			Thread.sleep(50);
			test01.stop();
			test02.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class testInfo implements Runnable {
	private boolean flag = true;

	@Override
	public void run() {
		int i = 0;
		while (flag) {
			System.out.println(Thread.currentThread().getName() + ":" + i); // 获取当前线程名称
			i++;
		}
	}

	public void stop() {
		this.flag = false;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值