7

I've seen many answers to this question but I'm still not sure.

One of them was "Java is preemptive". (The JVM schedules using a preemptive, priority based scheduling algorithm (usually round robin algorithm).

The second was that if 2 threads with the same priority run Java will not preempt and one thread could starve.

So now I wrote a program to check it out, I created 10 threads with minimum priority followed by 10 threads with maximum priority, the results were that I jump between all of the threads - meaning Java is preemptive even if 2 threads are with the same priority

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        for (int i=0;i<10;i++){
            Thread t=new Thread(new Dog(i));
            t.setPriority(Thread.MIN_PRIORITY);
            t.start();
        }

        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < 10; i++) {
            Thread g = new Thread(new Dog(i+10));
            g.setPriority(Thread.MAX_PRIORITY);
            g.start();
        }

    }
}

t

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author Matan2t
 */
public class Dog implements Runnable{
    public int _x=-1;
    public Dog(int x){
        _x=x;
    }
    @Override
    public void run(){
        while(true){
            System.out.println("My Priority Is : " + _x);
        }
    }

}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Matan Touti
  • 191
  • 2
  • 14

1 Answers1

10

I don't believe this is specified as precisely as either of the two quotes would suggest. All I could find was:

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread.

Beyond that, I believe the mechanics are platform- and JVM-specific. On the platforms that I am familiar with, the JVM uses OS threads and thus relies on the OS scheduler.

That said, given that all application threads by default have the same priority, it would be incredibly inconvenient if those threads were not capable of preempting one another.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • So, for conclusion: If my OS doesnt support threads scheduling, then the all same Priority threads will starve, since the JVM isnt doing anything. thanks – Matan Touti Jan 27 '13 at 09:23
  • 2
    If your OS doesn't support thread scheduling, you won't have a VM with native threads for this OS, and the VM will have to implement its own thread scheduling. What OS are you thinking about that doesn't have threads? – JB Nizet Jan 27 '13 at 09:30
  • Yes you are right with your question, maybe change the if in my question in suppose.. but never mind the "...the JVM uses OS threads and thus relies on the OS scheduler" answered me. Because the JVM relies on the OS scheduler which is preemptive, that is why i saw a confusing outputs. – Matan Touti Jan 27 '13 at 16:21
  • @JBNizet, HP NonStop's JRE is one implementation which does not use native threads. HP nonStop does not have threads at all and hence the JRE attempts to implement a thread scheduling using native processes. See the Multithreading section in the java manual . So yes, Thread scheduling is dependent on how JRE implements it .http://h10032.www1.hp.com/ctg/Manual/c02132394 – rajeshnair May 19 '15 at 11:27