线程的优先级(tPriority)
搞笑的歌曲线程可以划分优先级,优先级⾼的线程得到的CPU资源⽐较多,也就是CPU优先执⾏优先级⾼的线程对象中的任务。设置线程优先级有助于帮助线程规划器确定下⼀次选中哪⼀个线程优先执⾏。
java中优先级分为1-10个级别
线程优先级的继承特性 例如a线程启迪b线程,则b线程的优先级与a的⼀样。
代码说话:(很简单)
public class MyThread1 extends Thread {
@Override
public void run() {
System.out.println("MyThread1 run priority=" + Priority());
MyThread2 thread2 = new MyThread2();
thread2.start();
}
}
public class MyThread2 extends Thread {
@Override
public void run() {
System.out.println("MyThread2 run priority=" + Priority());
}
}
public static void main(String[] args) {
System.out.println("main thread begin priority="
+ Thread.currentThread().getPriority());
Thread.currentThread().tPriority(6);
System.out.println("main thread end priority="
+ Thread.currentThread().getPriority());
MyThread1 thread1 = new MyThread1();
thread1.start();
}
优先级具有规则性
public class MyThread1 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★thread 1 u time=" + (endTime - beginTime)); }
}
public class MyThread2 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
long addResult = 0;
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 50000; i++) {
Random random = new Random();
addResult = addResult + i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆thread 2 u time=" + (endTime - beginTime)); }
}
public static void main(String[] args) {八上生物思维导图
for (int i = 0; i < 5; i++) {
京骂MyThread1 thread1 = new MyThread1();
thread1.tPriority(1);
thread1.start();
MyThread2 thread2 = new MyThread2();
thread2.tPriority(10);
thread2.start();
}
}
做牛排
⾼优先级的线程总是先执⾏完
线程的优先级和代码的执⾏顺序没有关系
优先级具有随机性
⼀般优先级较⾼的线程先执⾏run()⽅法,但是这个不能说的但肯定,因为线程的优先级具有 “随机性”也就是较⾼线程不⼀定每⼀次都先执⾏完。ensure
public class MyThread1 extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Random random = new Random();
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★thread 1 u time=" + (endTime - beginTime));
}
}
public class MyThread2 extends Thread {
精神文化@Override
public void run() {
long beginTime = System.currentTimeMillis();
无尽空虚歌词
for (int i = 0; i < 1000; i++) {
Random random = new Random();
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆thread 2 u time=" + (endTime - beginTime));
}集邮总公司
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread1 thread1 = new MyThread1();
thread1.tPriority(5);
thread1.start();
MyThread2 thread2 = new MyThread2();
thread2.tPriority(6);
thread2.start();
}
}
守护线程介绍:
java 中有两种线程 ⼀个是⽤户线程,⼀个是守护(Daemon)线程
典型的守护线程就是垃圾回收线程,如果进程中没有⾮守护线程了,则守护线程⾃动销毁。守护线程作⽤就是为其他线程的运⾏提供便利的服务,⽐如GC。
public class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
try {
while (true) {
i++;
System.out.println("i=" + (i));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.tDaemon(true);//设置守护线程
thread.start();
Thread.sleep(5000);
System.out.println("我离开thread对象也不再打印了,也就是停⽌了!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}