首页 > 技术文章 > 线程优先级的获取及设置(外加名字设置方法和sleep方法)

EvanTheGreat 2021-02-24 11:08 原文

我们可以通过getPriority方法获得线程的优先级,对于主线程,因为它是在main方法里面执行的,不像MyThread那样可以通过对象获取,那么这里先获取线程,再得到它的优先级,来看看代码:

 1 package com.hw.thread0223;
 2 
 3 public class DemoTestThread {
 4     public static void main(String[] args) {
 5         
 6         Thread mainThread = Thread.currentThread();  //获取当前线程
 7         System.out.println("主线程的优先级是:"+mainThread.getPriority());
 8         
 9         MyThread thread = new MyThread();
10         System.out.println("MyThread的优先级是:"+thread.getPriority());
11         thread.start();
12         
13         for(int i = 0;i < 1000;i++)
14         {
15             System.out.println("MyMain:"+i);
16         }
17     }
18 }

 咱们的优先级是可以设置的,设置的时候直接给个数字就可以了,但是他也是有大小的,多大呢?我们可以写几行代码来看看:

1 System.out.println("默认优先级:"+Thread.NORM_PRIORITY);
2 System.out.println("最小优先级:"+Thread.MIN_PRIORITY);
3 System.out.println("最大优先级:"+Thread.MAX_PRIORITY);

 

 这个时候我们就知道了,设置优先级的时候,给的数字必须在1-10以内。我们可以设置一下优先级,看下是不是优先级更高的更有可能会被CPU执行:

 1 package com.hw.thread0223;
 2 
 3 public class DemoTestThread {
 4     public static void main(String[] args) {
 5         
 6         
 7         Thread mainThread = Thread.currentThread();  //获取当前线程
 8         mainThread.setPriority(10);
 9         
10         MyThread thread = new MyThread();
11         thread.setPriority(5);
12         thread.start();
13         
14         for(int i = 0;i < 1000;i++)
15         {
16             System.out.println("MyMain:"+i);
17         }
18     }
19 }

 

 这样就显而易见了,刚开始的时候主线程被执行的可能性更高。

其他的操作:

我们可以获取线程的名字,比如说:

MyThread.java:

 

 1 package com.hw.thread0223;
 2 
 3 public class MyThread extends Thread{
 4 
 5     public void run(){
 6         int j = 0;
 7         for(int i = 0;i < 100000;i++)
 8         {
 9             if(i % 2 == 0 && i % 6 == 0){
10                 j += i;
11             }else if(i % 2 == 0 && i % 6 != 0){
12                 j *= i;
13             }else if(i % 2 != 0 && i % 6 != 0){
14                 j -= i;
15             }
16         }
17         System.out.println(j);
18         System.out.println("MyThread:"+System.currentTimeMillis());
19     }
20 }

 

ThreadDemo2.java:

 1 package com.hw.thread0223;
 2 
 3 public class ThreadDemo2 {
 4     public static void main(String[] args) {
 5         MyThread t1 = new MyThread();
 6         MyThread t2 = new MyThread();
 7         
 8         System.out.println("t1的名字是:"+t1.getName());
 9         System.out.println("t2的名字是:"+t2.getName());
10         
11         t1.start();
12         t2.start();
13     }
14 }

 

 也可以自己设置名字,为了展示几种设置名字的方法,这里我们更改一下构造方法:

MyThread.java:

 1 package com.hw.thread0223;
 2 
 3 public class MyThread extends Thread{
 4     public MyThread(String name){
 5         super(name);
 6     }
 7     
 8     public MyThread(){
 9         
10     }
11     
12     public void run(){
13         int j = 0;
14         for(int i = 0;i < 100000;i++)
15         {
16             if(i % 2 == 0 && i % 6 == 0){
17                 j += i;
18             }else if(i % 2 == 0 && i % 6 != 0){
19                 j *= i;
20             }else if(i % 2 != 0 && i % 6 != 0){
21                 j -= i;
22             }
23         }
24         System.out.println(j);
25         System.out.println("MyThread:"+System.currentTimeMillis());
26     }
27 }

ThreadDemo2.java:

 1 package com.hw.thread0223;
 2 
 3 public class ThreadDemo2 {
 4     public static void main(String[] args) {
 5         MyThread t1 = new MyThread("线程1");
 6         MyThread t2 = new MyThread();
 7         MyThread t3 = new MyThread();
 8         
 9         System.out.println("t1的名字是:"+t1.getName());
10         System.out.println("t2的名字是:"+t2.getName());
11         t3.setName("线程3");
12         System.out.println("t3的名字是:"+t3.getName());
13         
14         t1.start();
15         t2.start();
16         t3.start();
17     }
18 }

 上面几种方法,可以直接在创建对象的时候设置,也可以调用setName()方法。

 此外,还有一个sleep方法,可以让线程暂停几秒之后再执行,这个时间可以自己设置。例如这样:

ThreadDemo2.java:

 1 package com.hw.thread0223;
 2 
 3 public class ThreadDemo2 {
 4     public static void main(String[] args) {
 5         MyThread t1 = new MyThread("线程1");
 6         MyThread t2 = new MyThread();
 7         MyThread t3 = new MyThread();
 8         
 9         try {
10             Thread.sleep(2000);
11         } catch (InterruptedException e) {
12             // TODO Auto-generated catch block
13             e.printStackTrace();
14         }
15         System.out.println("t1的名字是:"+t1.getName());
16         System.out.println("t2的名字是:"+t2.getName());
17         t3.setName("线程3");
18         System.out.println("t3的名字是:"+t3.getName());
19         
20         t1.start();
21         t2.start();
22         t3.start();
23     }
24 }

这样,在启动运行程序的时候,前两秒内啥都没有,两秒过后才会有反应。

推荐阅读