Threads On Normal Vs Maximum Priority

This question already has an answer here:

  • Java Thread priority has no effect 10 answers

  • 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.


    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..


    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.

    链接地址: http://www.djcxy.com/p/92056.html

    上一篇: 多线程访问内部类

    下一篇: 线程正常Vs最大优先级