Olá Pessoal.
Estou na reta final dos estudos para o SCJP 6, que será nesta quarta-feira. Hoje eu concluí o livro e estou com uma série de dúvidas hehe. Vou citar as dúvidas, os exemplos que fiz, não funcionaram. Primeiro, testando o notify() e wait():
Classe ThreadB
[code]public class ThreadB extends Thread {
int total;
public void run() {
synchronized (this) {
for (int i = 0; i < 100; i++) {
total += i;
}
notify();
}
}
}[/code]
Classe ThreadA
[code]public class ThreadA {
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();
synchronized (b) {
try {
System.out.println("Esperando b completar...");
b.wait();
} catch (InterruptedException e) {}
System.out.println("Total: " + b.total);
}
}
}[/code]
Por que a classe trava em “Esperando b completar…”?? Não era para o notify() liberar o recurso?
A outra dúvida com thread, é na parte do setPriority, como é mostrado abaixo:
Classe Contador
[code]public class Contador implements Runnable {
public void run() {
for (int i=0; i <= 100; i++) {
System.out.print(i + " ");
}
System.out.println("");
System.out.println(Thread.currentThread().getName());
System.out.println("");
}
}[/code]
Classe TestePrioridade
[code]public class TestePrioridade {
public static void main(String[] args) {
Contador c = new Contador();
Thread t1 = new Thread(c, "Thread-1");
t1.setPriority(Thread.MIN_PRIORITY);
Thread t2 = new Thread(c, "Thread-2");
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}[/code]
A outra dúvida é… Se eu setei o t2 como prioridade máxima, ele não deveria executar primeiro que o t1?
Essas foram algumas dúvidas sobre threads que eu obtive ao estudar o livro. Se alguém puder me ajudar, ficarei muito grato.
Abraços.