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);
}
}
}