Multithreading Java - Prioridade de Encadeamento
Alguém pode explicar como funciona a prioridade do thread em java. A confusão aqui é se o java não garante a implementação doThread
de acordo com a sua prioridade, então por que isso ésetpriority()
função usada para.
Meu código é o seguinte:
public class ThreadSynchronization implements Runnable{
public synchronized void run() {
System.out.println("Starting Implementation of Thread "+Thread.currentThread().getName());
for(int i=0;i<10;i++)
{
System.out.println("Thread "+Thread.currentThread().getName()+" value : "+i);
}
System.out.println("Ending Implementation of Thread "+Thread.currentThread().getName());
}
public static void main(String[] args) {
System.out.println("Program starts...");
ThreadSynchronization th1 = new ThreadSynchronization();
Thread t1 = new Thread(th1);
t1.setPriority(1);
synchronized(t1)
{
t1.start();
}
ThreadSynchronization th2 = new ThreadSynchronization();
Thread t2 = new Thread(th2);
t2.setPriority(9);
synchronized (t2) {
t2.start();
}
System.out.println("Program ends...");
}
}
No programa acima, mesmo se eu mudar a prioridade, não encontro diferença na saída. Também uma aplicação em tempo real de como a prioridade de thread pode ser usada seria de grande ajuda. Obrigado.