We help IT Professionals succeed at work.

java threads / set priority

Maymoon125
Maymoon125 asked
on
1,355 Views
Last Modified: 2013-11-23
Write a program that demonstrates that as a high priority thread executes, it will delay the execution of all lower priority threads.

would this do??

thanks for your help
public class SetPriority extends Object {
  private static Runnable makeRunnable() {
    Runnable r = new Runnable() {
      public void run() {
        for (int i = 0; i < 5; i++) {
          Thread t = Thread.currentThread();
          System.out.println("in run() - priority=" + t.getPriority()
              + ", name=" + t.getName());
          try {
            Thread.sleep(2000);
          } catch (InterruptedException x) {
          }
        }
      }
    }; 
    return r;
  } 
  public static void main(String[] args) {
    Thread threadA = new Thread(makeRunnable(), "threadA");
    threadA.setPriority(8);
    threadA.start(); 
    Thread threadB = new Thread(makeRunnable(), "threadB");
    threadB.setPriority(2);
    threadB.start(); 
    Runnable r = new Runnable() {
      public void run() {
        Thread threadC = new Thread(makeRunnable(), "threadC");
        threadC.start();
      }
    };
    Thread threadD = new Thread(r, "threadD");
    threadD.setPriority(7);
    threadD.start(); 
    try {
      Thread.sleep(3000);
    } catch (InterruptedException x) {
    } 
    threadA.setPriority(3);
    System.out.println("in main() - threadA.getPriority()="
        + threadA.getPriority());
  }
}

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
does this one work ?? i was hopping for some thing easy to understand...
CERTIFIED EXPERT

Commented:
Yes, this is working on my machine.
It explains how thread with lowest priority gets the lowest CPU percentage.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.