Jump to content

(Java)Questions about thread priority...


albertlee

Recommended Posts

Situation 1) setPriority(int adsa)

 

public class ThreadPrioDemo1 {

     static class Demo1 implements Runnable{
     public void run(){ //blablabla}

    }

    public static void main(String[] asdf){
         Runnable r = new Demo1();

         Thread myThread = new Thread(r);

         Thread mainT = Thread.currentThread();
         //mainT's priority is 5, so as myThread.

        mainT.setPriority(10);
       //mainT's priority to MAX, this simultaneously changes myThread to 10 as well.

       //or

        myThread.setPriority(10);
       //myThread priority to MAX, however, this does not change mainT to 10



    }

}

 

Question 1), why if main Thread is set to a certain priority, the thread it creates automatically change its priority to the same as well, but the reverse is not??

 

 

Situation 2) this is slightly more complex

 

 

public class ImplementingRunnable {

public static void main(String[] args) {

Thread mainT = Thread.currentThread();


	for(int ii=0 ; ii<args.length ; ii++) {
		Runnable r = new MyRunnable(args[ii]);
		Thread t = new Thread(r);
           t.setDaemon(true);
           //t.setPriority(Thread.MAX_PRIORITY);

		t.start();


	}
}

}

 

and

 

public class MyRunnable implements Runnable {

   public void test(){
   System.out.println("test");
   }

String message;

public MyRunnable(String msg) {
	message = msg;
}

public void run() {
	Thread me = Thread.currentThread();
	try {
		for(int ii=0 ; ii><10 ; ii++) {
		    //case (1) me.setPriority(Thread.MIN_PRIORITY);
			me.sleep(3);
			System.out.println(message);
			//case (2) me.setPriority(Thread.MIN_PRIORITY);
		}
	} catch (InterruptedException ie) {
	}
}
}

 

In class ImplementingRunnable, how is the commented code line related or counter-related with the commented case 1 code line in MyRunnable??

 

Please help

 

thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.