Czy Java jest wyprzedzająca?
Widziałem wiele odpowiedzi na to pytanie, ale wciąż nie jestem pewien.
Jedną z nich była „Java jest prewencyjna”. (Harmonogram JVM korzysta z algorytmu planowania z wyprzedzeniem, opartego na priorytetach (zwykle algorytm round robin).
Drugim było to, że jeśli 2 wątki o tym samym priorytecie uruchomią Javę, nie będą wyprzedzać, a jeden wątek może umrzeć głodem.
Więc teraz napisałem program, aby to sprawdzić, stworzyłem 10 wątków z minimalnym priorytetem, a następnie 10 wątków z maksymalnym priorytetem, wyniki były takie, że przeskakuję między wszystkimi wątkami - co oznacza, że Java jest wyprzedzająca, nawet jeśli 2 wątki są takie same priorytet
/*
* 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);
}
}
}