Priority of Threads

Source: Internet
Author: User
Tags count int size thread

The

Thread's priority (Priority) tells the debugger how important the thread is. If a large number of threads are blocked and are waiting to be run, the debugger first runs the thread with the highest priority. However, this does not mean that a lower-priority thread will not run (in other words, it will not cause a deadlock because of a priority). If the thread has a lower priority, it simply means that it is allowed to run less.
Use the GetPriority () method to read the priority of a thread and change it with setpriority (). In the following patch, you will find that the counter count slows down because the thread they are associated with has a lower priority:
 

: Counter5.java//Adjusting the priorities of threads import java.awt.*;
Import java.awt.event.*;

Import java.applet.*; Class Ticker2 extends Thread {private button b = New button ("Toggle"), incpriority = New button ("Up"), Dec
  Priority = new Button ("Down"); Private TextField t = new TextField (ten), PR = new TextField (3);
  Display priority private int count = 0;
  Private Boolean runflag = true;
    Public Ticker2 (Container c) {B.addactionlistener (New Togglel ());
    Incpriority.addactionlistener (New UPL ());
    Decpriority.addactionlistener (New downl ());
    panel p = new Panel ();
    P.add (t);
    P.add (PR);
    P.add (b);
    P.add (incpriority);
    P.add (decpriority);
  C.add (P);
    Class Togglel implements ActionListener {public void actionperformed (ActionEvent e) {runflag =!runflag; Class UPL implements ActionListener {public void actionperformed (ActionEvent e) {int newpriority =
     GetPriority () + 1; if (newpriority > thread.max_priority) newpriority = thread.max_priority;
    SetPriority (newpriority); Class DOWNL implements ActionListener {public void actionperformed (ActionEvent e) {int newpriority = g
      Etpriority ()-1;
      if (Newpriority < thread.min_priority) newpriority = thread.min_priority;
    SetPriority (newpriority);
        } public void Run () {while (true) {if (Runflag) {T.settext (integer.tostring (count++));
      Pr.settext (Integer.tostring (GetPriority ()));
    } yield (); }} public class Counter5 extends Applet {private button start = New button ("Start"), Upmax = New button (
  "Inc Max Priority"), Downmax = New button ("Dec max Priority");
  Private Boolean started = false;
  private static final int SIZE = 10;
  Private ticker2[] s = new ticker2[size];
  Private TextField MP = new TextField (3); public void Init () {for (int i = 0; i < s.length; i+)+) S[i] = new Ticker2 (this);
    Add (New Label ("max_priority =" + thread.max_priority));
    Add (New Label ("min_priority =" + thread.min_priority));
    Add (New Label ("Group Max Priority =")); 
    Add (MP);
    Add (start); Add (Upmax);
    Add (Downmax);
    Start.addactionlistener (New Startl ());
    Upmax.addactionlistener (New Upmaxl ());
    Downmax.addactionlistener (New Downmaxl ());
    Showmaxpriority ();
    Recursively display parent thread groups:threadgroup parent = S[0].getthreadgroup (). GetParent (); while (parent!= null) {Add (New Label ("parent Threadgroup max priority =" + parent.getmaxpriority (
      )));
    Parent = Parent.getparent ();
  } is public void Showmaxpriority () {Mp.settext (integer.tostring s[0].getthreadgroup (). getmaxpriority ());
        Class Startl implements ActionListener {public void actionperformed (ActionEvent e) {if (!started) {
        started = true; for (int i = 0; i < s.length;
      i++) S[i].start (); 
        Class Upmaxl implements ActionListener {public void actionperformed (ActionEvent e) {int MAXP =
      S[0].getthreadgroup (). getmaxpriority ();
      if (++maxp > thread.max_priority) maxp = thread.max_priority;
      S[0].getthreadgroup (). setmaxpriority (MAXP);
    Showmaxpriority (); 
        Class Downmaxl implements ActionListener {public void actionperformed (ActionEvent e) {int MAXP =
      S[0].getthreadgroup (). getmaxpriority ();
      if (--maxp < thread.min_priority) Maxp = thread.min_priority;
      S[0].getthreadgroup (). setmaxpriority (MAXP);
    Showmaxpriority ();
    } public static void Main (string[] args) {Counter5 applet = new Counter5 ();
    Frame aframe = new Frame ("Counter5"); Aframe.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {System.
        Exit (0);
    }
      }); Aframe.adD (applet, borderlayout.center);
    Aframe.setsize (300, 600);
    Applet.init ();
    Applet.start ();
  Aframe.setvisible (TRUE); }
} ///:~

Ticker takes the form constructed earlier in this chapter, but there is an extra TextField (text field) that shows the priority of the thread, as well as two additional buttons for human improvement and lower priority.
Also note the use of yield (), which automatically returns control to the debugger (mechanism). If this is not done, the multithreaded mechanism will still work, but we will find that it is running slowly (try deleting the call to yield ()). You can also call sleep (), but if you do that, the frequency of the count changes to the duration of the control () rather than the priority.
Init () in COUNTER5 creates an array of 10 Ticker2, and their buttons and input fields (text fields) are placed into the form by the Ticker2 Builder. Counter5 adds a new button to start everything and to increase and decrease the maximum priority of the thread group. In addition, there are labels that show the maximum and minimum priority a thread can take, as well as a special text field to display the maximum priority of the thread group (in the next section, we'll cover the thread group issues in a comprehensive way). Finally, the priority of the parent thread group is also displayed as a label.
When you press the "Up" or "Down" button, you first get the Ticker2 current priority and then increase or decrease accordingly.
There are a few things we can notice when we run the program. First, the thread group's default priority is 5. Each thread has a default priority of 5, even before the thread is started (or, before the thread is created, it requires proper modification of the code) to reduce the maximum priority to below 5.
The simplest test is to get a counter that lowers its priority to 1, and it should be observed that its counting frequency slows significantly. Now try to raise the priority again, and you can raise the priority of the thread group, but you can't go any higher. Now reduce the priority of the thread Group two times. The priority of a thread does not change, but if you try to improve or lower it, you will find that the priority automatically becomes the priority of the thread group. In addition, the new thread still has a default priority, even if it is higher than the group's priority (in other words, do not expect to use group precedence to prevent new threads from having a higher priority than the existing ones).
Finally, try to increase the group's maximum priority. It can be found that there is no effect in doing so. We can only reduce the maximum priority of the thread group, not increase it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.