6.线程优先级:Thread中有哪几个线程优先级常量???

6.线程优先级:Thread中有哪几个线程优先级常量???线程优先级相关常用方法有哪几个???

学习:第7遍


1.Thread中有哪几个线程优先级常量???

线程优先级范围[1,10], 优先级越高,执行机会越大,只是概率,并不一定
最大优先级即10:Thread.MAX_PRIORITY
最小优先级即1:Thread.MIN_PRIORITY
普通优先级即5:Thread.NORM_PRIORITY


2.线程优先级相关常用方法有哪几个???

方法一:thread.setPriority(10)
作用:设置线程优先级

方法二:thread.getPriority()
作用:得到线程优先级

方法三:Thread.currentThread()
作用:得到当前线程,静态方法

方法四:System.currentTimeMillis()
作用:得到系统的当前时间,long型表示,静态方法


/*
 * 线程优先级
 * 范围[1,10]
 * 优先级越高,执行机会越大,只是概率,并不一定
 */
public class Test{
	public static void main(String[] args) {
		// System.out.println("最大优先级:"+Thread.MAX_PRIORITY);
		// System.out.println("最小优先级:"+Thread.MIN_PRIORITY);
		// System.out.println("普通优先级:"+Thread.NORM_PRIORITY);
		// System.out.println(Thread.currentThread().getPriority());

		for (int i = 1; i <= 20; i++) {
			MyThread3 t1 = new MyThread3();
			MyThread3 t2 = new MyThread3();
			t1.setPriority(10);
			t2.setPriority(1);
			t1.start();
			t2.start();
		}

	}
}

class MyThread3 extends Thread {

	@Override
	public void run() {
		long start = System.currentTimeMillis();
		String str = "";
		for (int i = 1; i <= 10000; i++) {
			str += i;
		}
		long end = System.currentTimeMillis();
		System.out.println(getPriority() + "-----------" + (end - start) + "ms");
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值