2

I have been working on threads. I have found that normal priority is five. But I just like to set the priority Maximum as ten.

The jobs running on normal and maximum priority changed the meaning,Even-though I specified the priority to my threads. It's simply running as normal and maximum as mixed case.

If any stuffs about JVM Vs OS, explain me fully.

Why is this happening? I am little bit confused on the JVM behavior. Some deep explanation will make me clear. Code is given below!

class Count implements Runnable
{
    public void run()
    {
        for(int i=1;i<=100;i++)
        {
            if(i%10==1)
            {
                System.out.println("10 counts have been finished");
            }
            System.out.println("The numbers are:"+i+"From"+Thread.currentThread().getName());
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                System.out.println(e);
            }
        }
    }
}
public class ThreadEx
{
    public static void main(String[] args) 
    {
        Count c = new Count();
        Thread t = new Thread(c);
        Thread t1 = new Thread(c);
        t.setName("Sridhar Job");
        t.start();
        t1.setName("Bob Job");
        t1.setPriority(10);
        t1.start(); 
    }
}

All the stuffs which I read as Threads working is not guaranteed. What is the core relation between JVM Threading and OS Threading?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
JAVA Beginner
  • 389
  • 3
  • 15
  • Well, when the high priority thread isn't doing anything, why *wouldn't* it use that time to run the normal priority one? – user253751 Mar 09 '16 at 03:52

3 Answers3

1

That happens because of

Thread.sleep(1000);

if your higher priority thread is in sleep mode then any other thread get chance to execute it

remove this block and check "Bob Job" finish first.

Darshan Patel
  • 3,176
  • 6
  • 26
  • 49
1

See... there will be one or more processrs in your system... Now, whenever you run a java program, each program will have its own instance of the JVM.. Whenever the program tries to do something, the underlying JVM makes calls to the OS to achieve it.. The JVM in itself cannot accomplish certain tasks, it just forwards those calls to the OS.. The JVM CANNOT control everything...

having said this, coming back to your question, you have set the thread priority as 5 and 10 respectively.. The JVM "does not execute threads". The threads are sheduled by the scheduler (OS) and then executed. the JVM can just say to the OS that the priority of this thread is high, but ultimately its left to the OS to decide how much priority should be given to a thread . For the OS, the JVM is just another process asking for resources..

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Raising the priority only helps if

  • you are close to 100% CPU utilisation and the OS schedule needs to make a choice as to which thread to run. These days with lots of free CPUs, this is unlikely
  • AND you have root or administrator privileged.
  • AND you don't really care if it always works.

This combination makes them pretty useless, not more than a hint which is usually ignored. I would treat it as nothing more than a comment which is visible when you dump your thread stacks.

Most of the time, using less busy threads than you have CPUs is better solution.

I am little bit confused on the JVM behavior

In many cases, like this one, the JVM just passes the parameters to the OS to let it do it's job. The only thing the JVM does is print the priority when you dump the threads.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130