Multithread priority of java

Posted by username123 on Wed, 17 Jul 2019 23:03:43 +0200

Reprinted please indicate the source

http://blog.csdn.NET/pony_maggie/article/details/43889225


Author: Pony

 

Java Thread priority ranges from 1 (Thread.MIN_PRIORITY) to 10 (Thread.MAX_PRIORITY). If no settings exist, The default priority for threads is NORM_PRIORITY. This value is 5.

 

demo creates 10 thread instances that show the current priority of each thread and can modify that priority. The effect of the program is as follows.


 

As shown in the figure, 10 ticker instances can be up and down to control their priority and can be displayed in real time.

  1. class Ticker2 extends Thread  
  2. {  
  3.     private JButton b = new JButton("Toggle");  
  4.     private JButton incPriority = new JButton("up");  
  5.     private JButton decPriority = new JButton("dwon");  
  6.       
  7.     private JTextField t = new JTextField(10),  
  8.     pr = new JTextField(3); //display priority  
  9.       
  10.     private int count = 0;  
  11.     private boolean runFlag = true;  
  12.       
  13.     public Ticker2(Container c)  
  14.     {  
  15.         b.addActionListener(new ToggleL());  
  16.         incPriority.addActionListener(new UpL());  
  17.         decPriority.addActionListener(new DownL());  
  18.           
  19.         //The purpose of the new panel here is to group, because ticker s create arrays so that each instance has its own panel.  
  20.         JPanel p = new JPanel();  
  21.         p.add(t);  
  22.         p.add(pr);  
  23.           
  24.         p.add(b);  
  25.         p.add(incPriority);  
  26.         p.add(decPriority);  
  27.           
  28.         c.add(p);//It should be OK to use container s directly here without adding panel s.  
  29.         //  
  30.           
  31.           
  32.           
  33.     }  
  34.     class ToggleL implements ActionListener  
  35.     {  
  36.         public void actionPerformed(ActionEvent e)  
  37.         {  
  38.             runFlag = !runFlag;  
  39.         }  
  40.     }  
  41.     class UpL implements ActionListener  
  42.     {  
  43.         public void actionPerformed(ActionEvent e)  
  44.         {  
  45.             //Java threads have priority ranges from 1 (Thread.MIN_PRIORITY) to 10 (Thread.MAX_PRIORITY).  
  46.             int newPriority = getPriority() + 1//Each increment  
  47.             if(newPriority > Thread.MAX_PRIORITY)  
  48.             {  
  49.                 newPriority = Thread.MAX_PRIORITY;  
  50.             }  
  51.             setPriority(newPriority);  
  52.         }  
  53.     }  
  54.       
  55.     class DownL implements ActionListener  
  56.     {  
  57.         public void actionPerformed(ActionEvent e)  
  58.         {  
  59.             int newPriority = getPriority() - 1;  
  60.             if(newPriority < Thread.MIN_PRIORITY)  
  61.             {  
  62.                 newPriority = Thread.MIN_PRIORITY;  
  63.             }  
  64.             setPriority(newPriority);  
  65.         }  
  66.     }  
  67.       
  68.     public void run()  
  69.     {  
  70.         while(true)  
  71.         {  
  72.             if(runFlag)  
  73.             {  
  74.                 t.setText(Integer.toString(count++));  
  75.                 pr.setText(Integer.toString(getPriority()));  
  76.             }  
  77.             //The yield method gives the same priority thread an opportunity to execute.  
  78.             yield();  
  79.         }  
  80.     }  
  81. }  



For each ticker, the first box shows a counter indicating how many execution opportunities the current thread has received, and the second box shows the current priority of the thread. Notice that all threads start with a priority of 4, because the thread group has a concept of the highest priority, and all threads in the group can't exceed that priority. You can try clicking the up button, the maximum is to group max priority. This value is 4.

 

  1. public void init()  
  2.     {  
  3.         Container cp = getContentPane();  
  4.         cp.setLayout(new FlowLayout());  
  5.         for(int i = 0; i < s.length; i++)  
  6.         {  
  7.             s[i] = new Ticker2(cp);  
  8.         }  
  9.           
  10.         cp.add(new JLabel("MAX_PRIORITY = " + Thread.MAX_PRIORITY));  
  11.         cp.add(new JLabel("MIN_PRIORITY = " + Thread.MIN_PRIORITY));  
  12.         cp.add(new JLabel("Group max priority"));  
  13.           
  14.         cp.add(mp);  
  15.         cp.add(start);  
  16.         cp.add(upMax);  
  17.         cp.add(downMax);  
  18.         start.addActionListener(new StartL());  
  19.         upMax.addActionListener(new UpMaxL());  
  20.         downMax.addActionListener(new DownMaxL());  
  21.         showMaxPriority();  
  22.           
  23.         //recursively display parent thread group  
  24.         ThreadGroup parent = s[0].getThreadGroup().getParent();  
  25.         while(parent != null)  
  26.         {  
  27.             cp.add(new Label("parent threadgroup max priority = " +  
  28.                     parent.getMaxPriority()));  
  29.             parent = parent.getParent();  
  30.         }  
  31.           
  32.           
  33.     }  


 

In Java, each thread belongs to a member of a thread group management. For example, if a thread is generated in the main() main workflow of the main function, the generated thread belongs to a member of the main thread group management. Simply put, a ThreadGroup is a class of threads that manages threads. This class is the java.lang.ThreadGroup class.

 

Thread.MAX_PRIORITY should be the highest priority of thread group in theory, but the actual situation is often more complex. Here are excerpts and translations from JAVAMEX->. Javathreading introduction -> Thread priorioties

 

For thread priority, we need to pay attention to:
* Thread.setPriority() probably doesn't do anything at all, that's with you. operating system Relevant to Virtual Machine Version


* Thread priority may have different meanings for different thread schedulers, which may not be your intuitive guess. In particular, priority does not necessarily refer to CPU sharing. In UNIX systems, priority can more or less be considered CPU allocation, but not in Windows.


* Thread priority is usually a combination of global and local priority settings. Java's setPriority() method applies only to local priorities. In other words, you can't prioritize the whole range of possibilities. (This is usually a way of protection, and you probably don't want the mouse pointer threads or audio data processing threads to be preempted by other random user threads.)


* Different systems have different ranges of thread priority, but Java defines 10 levels (1-10). This makes it possible for several threads to have different priorities in one operating system, but the same priorities in another (and therefore unexpected behavior)


* Operating systems may (and usually do) add proprietary behavior to threads based on their priority (e.g. "only give a quantum boost if the priority is below X"). Again, there are some differences in the definition of priority between different systems.


* Thread schedulers in most operating systems actually perform temporary operations on thread priorities from a strategic perspective (e.g. when a thread receives an event or I/O it is waiting for), the operating system usually knows the most, and trying to control priorities manually may only interfere with the system.


* Your application usually does not know which threads other processes are running, so the impact of changing the priority of a thread is difficult to predict for the entire system. For example, you may find that you have a low-priority thread that you expect to occasionally run in the background that hardly runs because a virus monitor runs at a slightly higher priority (but still below normal priority) and you can't predict the performance of your program, which will depend on your client. The antivirus program used varies.

Topics: Java Unix less Windows