Concurrent programming study notes (four priority of the thread)

table of Contents:

  • Thread priority
  • Thread priority features
  • Daemon thread
  • to sum up

Thread priority:

1, in java thread priorities range from 1 to 10, the default is 5, the larger the value the higher the priority.

2, high-priority thread of execution will give priority to low priority thread, but it just takes precedence only.

Thread priority features:

1, inheritance

If thread A executed in the thread B , then thread A and thread B has the same priority .

. 1  public  class MyThread1 the extends the Thread {
 2      @Override
 . 3      public  void RUN () {
 . 4          // output thread level 
. 5          System.out.println ( "MyThread1 the Priority =" + the this .getPriority ());
 . 6          // start threads MyThread2 
. 7          myThread2 = MyThread2 new new MyThread2 ();
 . 8          myThread2.start ();
 . 9      }
 10 }
1 public class MyThread2 extends Thread {
2     @Override
3     public void run() {
4         System.out.println("MyThread2 Priority = " + this.getPriority());
5     }
6 }
1 public class ThreadPriority {
2     public static void main(String[] args) {
3         System.out.println("once Main Thread Priority = " + Thread.currentThread().getPriority());
4         Thread.currentThread().setPriority(10);
5         System.out.println("twice Main Thread Priority = " + Thread.currentThread().getPriority());
6         MyThread1 myThread1 = new MyThread1();
7         myThread1.start();
8     }
9 }

If the comment line ThreadPriority category 4,5 , the result is:

once Main Thread Priority = 5

twice Main Thread Priority = 5

MyThread1 Priority = 5

MyThread2 Priority = 5

Because the thread priority default is 5.

 

If the normal execution ThreadPriority the main method, the result is:

once Main Thread Priority = 5

twice Main Thread Priority = 10

MyThread1 Priority = 10

MyThread2 Priority = 10

As can be seen from the results thread has inherited.

2, regularity and randomness

) CPU resources as far as possible to high-priority thread, but a higher priority thread is not executing the first .

) Even if the thread does not set priorities, we can not guarantee the thread has run, running threads of randomness.

 1 public class MyThread1 extends Thread {
 2     @Override
 3     public void run() {
 4         long start = System.currentTimeMillis();
 5         System.out.println("------1------ thread 1 start running");
 6         long count = 0;
 7         for (int i = 0; i < 10; i++) {
 8             for (int j = 0; j < 50000; j++) {
 9                 Random random = new Random();
10                 random.nextInt();
11                 count = count + i;
12             }
13         }
14         long end = System.currentTimeMillis();
15         System.out.println("------1------ thread 1 use time = " + (end - start));
16     }
17 }
 1 public class MyThread2 extends Thread {
 2     @Override
 3     public void run() {
 4         long start = System.currentTimeMillis();
 5         System.out.println("------2------ thread 2 start running");
 6         long count = 0;
 7         for (int i = 0; i < 10; i++) {
 8             for (int j = 0; j < 50000; j++) {
 9                 Random random = new Random();
10                 random.nextInt();
11                 count = count + i;
12             }
13         }
14         long end = System.currentTimeMillis();
15         System.out.println("------2------ thread 2 use time = " + (end - start));
16     }
17 }
 1 public class ThreadPriority {
 2     public static void main(String[] args) {
 3         for (int i = 0; i < 10; i++) {
 4             MyThread1 myThread1 = new MyThread1();
 5             myThread1.setPriority(1);
 6             MyThread2 myThread2 = new MyThread2();
 7             myThread2.setPriority(10);
 8             myThread1.start();
 9             myThread2.start();
10         }
11     }
12 }

:( excessive operating results, the article does not show !!! Σ (゚ Д ゚ Techno) Techno)

From the results we can see the high priority thread 1 2 is not necessarily complete than the first execution threads.

Daemon thread:

1, the thread is divided into two types, one is the thread created by developers, such threads commonly called user thread ; one is the JVM a background thread, these threads commonly called daemon threads , such as garbage collection thread .

2, by isDaemon () method may return to distinguish whether a daemon thread function true was daemon thread , and vice versa for the user thread.

3, when the user thread JVM implementation of the latter will end the program , because the daemon thread is a background thread, a thread when the whole thing after the implementation of the user there is no need to guard.

4, through Thread.setDaemon () Set the thread daemon thread, but needs to be specified before the execution start () method, otherwise it will throw IllegalThreadStateException exception.

 1 public class CommonThread extends Thread {
 2     @Override
 3     public void run() {
 4         for (int i = 0; i < 5; i++) {
 5             System.out.println("用户线程第" + i + "次执行!");
 6             try {
 7                 Thread.sleep(10);
 8             } catch (Exception e) {
 9                 e.printStackTrace();
10             }
11         }
12     }
13 }
 1 public class MyDaemon implements Runnable {
 2     @Override
 3     public void run() {
 4         for (long i = 0; i < 100; i++) {
 5             System.out.println("守护线程第" + i + "次执行!");
 6             try {
 7                 Thread.sleep(10);
 8             } catch (Exception e) {
 9                 e.printStackTrace();
10             }
11         }
12     }
13 }
 1 public class DaemonThreadDemo {
 2     public static void main(String[] args) {
 3         Thread t1 = new CommonThread();
 4         Thread t2 = new Thread(new MyDaemon());
 5         //设置为守护线程
 6         t2.setDaemon(true);
 7         t2.start();
 8         t1.start();
 9     }
10 }

operation result:

User thread 0 execution!

Daemon thread 0 execution!

1st execution daemon thread!

User thread 1st execution!

User thread 2nd execution!

2nd execution daemon thread!

3rd daemon thread execution!

User thread 3rd execution!

User thread fourth execution!

4th execution daemon thread!

5th daemon thread execution!

Daemon thread should be required to perform 100 times, but in fact only performed six times, so we can see that after the implementation of user threads JVM will stop the program, a daemon thread will not perform anymore.

to sum up:

1, there is a thread priority points, from 1 to 10

2, a higher priority than the thread as low priority thread to run 

3, thread priority features: inheritance, regularity, randomness

4, Java threads are divided into two kinds: the user thread and thread guard

5, daemon thread in the JVM after all threads end user exits

6, the user can manually create a daemon thread

Guess you like

Origin www.cnblogs.com/bzfsdr/p/11566593.html