多线程学习--线程优先级
线程的优先级:继承性,规则性,随机性
- 线程的优先级具有继承性. 如,线程A启动线程B,则B和A优先级一样
- 线程的优先级具有规则性. CPU尽量倾向于把资源优先级高的线程
- 线程的优先级具有随机性. 优先级不等同于执行顺序,二者关系不确定
优先级范围:0~10
继承性:
class MyThread extends Thread {
@Override
public void run() {
System.out.println("MyThread17_1 run priority=" + this.getPriority());
MyThread2 myThread2 = new MyThread2 ();
myThread2 .start();
}
}
class MyThread2 extends Thread {
@Override
public void run() {
System.out.println("MyThread17_2 run priority=" + this.getPriority());
}
}
public class test{
public static void main(String[] args) {
System.out.println("main thread begin priority =" + Thread.currentThread().getPriority());
//Thread.currentThread().setPriority(6);
System.out.println("main thread end priority =" + Thread.currentThread().getPriority());
MyThread myThread = new MyThread ();
myThread .start();
}
}
结果:
//Thread.currentThread().setPriority(6);
输出:
main thread begin priority =5
main thread end priority =5
MyThread run priority=5
MyThread2 run priority=5
--------------------
Thread.currentThread().setPriority(6);
输出:
main thread begin priority =5
main thread end priority =6
MyThread run priority=6
MyThread2 run priority=6
规则性:
class MyThread 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();
random.nextInt();
addResult += i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★ thread1 use time = " + (endTime - beginTime));
}
}
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();
random.nextInt();
addResult += i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆ thread2 use time = " + (endTime - beginTime));
}
}
public class Run18_priority02 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread myThread = new MyThread ();
myThread .setPriority(10);
//myThread .setPriority(1);
myThread .start();
MyThread2 myThread2 = new MyThread2 ();
//myThread2 .setPriority(10);
myThread2 .setPriority(1);
myThread2 .start();
}
}
}
结果:
myThread.setPriority(10);
myThread2.setPriority(1);
输出:
☆☆☆☆☆ thread2 use time = 202
★★★★★ thread1 use time = 208
★★★★★ thread1 use time = 255
★★★★★ thread1 use time = 261
★★★★★ thread1 use time = 268
★★★★★ thread1 use time = 270
☆☆☆☆☆ thread2 use time = 321
☆☆☆☆☆ thread2 use time = 338
☆☆☆☆☆ thread2 use time = 344
☆☆☆☆☆ thread2 use time = 35
-------------------------
★★★★★ thread1 use time = 121
★★★★★ thread1 use time = 232
☆☆☆☆☆ thread2 use time = 268
☆☆☆☆☆ thread2 use time = 292
★★★★★ thread1 use time = 322
☆☆☆☆☆ thread2 use time = 325
★★★★★ thread1 use time = 387
☆☆☆☆☆ thread2 use time = 390
★★★★★ thread1 use time = 395
☆☆☆☆☆ thread2 use time = 409
----------------------------------------------------
----------------------------------------------------
myThread.setPriority(1);
myThread2.setPriority(10);
输出:
☆☆☆☆☆ thread2 use time = 393
☆☆☆☆☆ thread2 use time = 455
☆☆☆☆☆ thread2 use time = 496
☆☆☆☆☆ thread2 use time = 509
☆☆☆☆☆ thread2 use time = 510
★★★★★ thread1 use time = 511
★★★★★ thread1 use time = 554
★★★★★ thread1 use time = 558
★★★★★ thread1 use time = 561
★★★★★ thread1 use time = 569
---------------
☆☆☆☆☆ thread2 use time = 214
☆☆☆☆☆ thread2 use time = 230
☆☆☆☆☆ thread2 use time = 237
★★★★★ thread1 use time = 257
☆☆☆☆☆ thread2 use time = 261
☆☆☆☆☆ thread2 use time = 271
★★★★★ thread1 use time = 350
★★★★★ thread1 use time = 369
★★★★★ thread1 use time = 339
★★★★★ thread1 use time = 382
随机性:运行顺序。
class MyThread 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();
random.nextInt();
addResult += i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★ thread1 use time = " + (endTime - beginTime));
}
}
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();
random.nextInt();
addResult += i;
}
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆ thread2 use time = " + (endTime - beginTime));
}
}
public class Run18_priority02 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
MyThread myThread = new MyThread ();
myThread .setPriority(5);
myThread .start();
MyThread2 myThread2 = new MyThread2 ();
myThread2 .setPriority(6);
myThread2 .start();
}
}
}
结果:
☆☆☆☆☆ thread2 use time = 981
★★★★★ thread1 use time = 992
★★★★★ thread1 use time = 996
☆☆☆☆☆ thread2 use time = 1034
☆☆☆☆☆ thread2 use time = 1137
☆☆☆☆☆ thread2 use time = 1138
☆☆☆☆☆ thread2 use time = 1150
★★★★★ thread1 use time = 1182
★★★★★ thread1 use time = 1185
★★★★★ thread1 use time = 1188
还没有评论,来说两句吧...