Jump to content

(Java)Questions about thread priority...

Featured Replies

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

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.