1

I gave thread 1 high priority and thread 2 low priority.

Now, when I executed the code. Thread one will go to sleep for 500ms then thread two is called and it will also go to 500 ms sleep.

Once t1 and t2 woke up, let's assume because of some system error both woke up at the same time. The thread with MAX_PRIORITY should run first. But, here why t2 is running?

*sometimes t1 will run and some times t2 will run there is no guarantee

public class JavaThread_priority_name {
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            public void run(){
                for (int i = 0; i < 5; i++) {
                    try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
                    System.out.print("High -> " + (i+1));
                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for (int i = 0; i < 5; i++) {
                    try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}
                    System.out.println("Low -> " + (i+1));
                }
            }
        };

        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);

        t1.start();
        t2.start();
    }
}

output

Low -> 1
High -> 1
Low -> 2
High -> 2
Low -> 3
High -> 3
High -> 4
Low -> 4
Low -> 5
High -> 5
Vikas Acharya
  • 3,550
  • 4
  • 19
  • 52
  • 1
    Thread.sleep() is everything but accurate. You can't really use it for synchronization. –  Dec 18 '20 at 08:34
  • @Hulk hey Hulk, I came from there only. It didn't answered my question. – Vikas Acharya Dec 18 '20 at 08:34
  • @tibetiroka Thank you, can you tell me why the scheduler picks t2 first. – Vikas Acharya Dec 18 '20 at 08:35
  • @VikasAcharya this is highly OS and implementation specific. There are basically no guarantees in this area - we could only guess. – Hulk Dec 18 '20 at 08:36
  • @Hulk are you saying scheduler working is dependent on OS. i.e scheduler is different in different OS – Vikas Acharya Dec 18 '20 at 08:38
  • @VikasAcharya yes, but it is even more complicated than that. Basically, your output doesn't really prove that `t2` "really" got scheduled before `t1` - all we know is that it got to print the output first. But at least theoretically, there could have been multiple context switches before it got there. And given the `sleep`s, that it even likely. – Hulk Dec 18 '20 at 08:41
  • @Hulk Thank you, Is there any easy docs on that. I referred some books and I didn't get understood anything coz I'm not from a computer background. – Vikas Acharya Dec 18 '20 at 08:43
  • The question is why you are so interested in that. Why is that relevant? – Dorian Gray Dec 18 '20 at 08:48
  • 2
    @VikasAcharya well, these are quite advanced topics to begin with. Understanding concurrency is one of the hardest things in programming. A good rule mof thumb is that as soon as there is concurrency, the order of events is no longer guaranteed. If you need to know it, you need to establish what is called a "happens-before" relation between events, by using some form of synchronization. – Hulk Dec 18 '20 at 08:48
  • Basically, if you want to study this from an academic point of view, you could try starting with how the language spec specifies the [Memory Model](https://docs.oracle.com/javase/specs/jls/se15/html/jls-17.html#jls-17.4): "A memory model describes, given a program and an execution trace of that program, whether the execution trace is a legal execution of the program." – Hulk Dec 18 '20 at 09:04
  • Even if the threads where waking up on the same time (which is a very big if), given your CPU has multiple cores, it is entirely possible both threads were scheduled to run at the same time, so the priority might not have been relevant at all. – Mark Rotteveel Dec 18 '20 at 16:24
  • @DorianGray because of corona I lost my job and now I'm attending the interview and got rejected. They asked this question – Vikas Acharya Dec 19 '20 at 03:23
  • @VikasAcharya Well, I'm sorry to hear that. My question was just to see if there is any practical application behind that question. Appearently, it is just of theoretical interest. – Dorian Gray Dec 22 '20 at 17:41

0 Answers0