[size=12][size=18]Tenho estas três classes: A 1ª representa uma árvore(vetor de vizinhança), a 2ª é a thread e a 3ª classe é a principal. As threds criadas na classe principal deveriam ser executas em paralelo. Alguém poderia me ajudar a saber se estão realmente em paralelo??? Ou uma forma melhor de colocá-las em paralelo.
Desde já agradeço a ajuda.[/size][/size]
[code]package pointerJumping;
import java.util.Scanner;
public class Arvore {
// Vetor que representa uma árvore.
int vizinhos[];
int num_vertices;
// Construtor
Arvore(int tamanho) {
vizinhos = new int[tamanho];
num_vertices = tamanho;
}
// Seleciona os vizinhos.
public void setParent(){
for (int i=0; i < vizinhos.length ;i++){
System.out.println("Diga o vizinho do nó "+i+":");
Scanner teclado = new Scanner(System.in);
vizinhos[i]= teclado.nextInt();
}
}
// Imprime a raiz de um dado nó.
public void imprime(int no){
System.out.println("Nó "+no+ " possui raiz "+vizinhos[no]);
}
}[/code]
[code]package pointerJumping;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Pointer_Jumping implements Runnable {
private String threadName;
private Arvore arv;
private int indice;
private Lock pointerLock = new ReentrantLock();//ReentrantLock emplementa a inteface Lock.
public Pointer_Jumping(String name, Arvore arvore, int ind){
threadName = name;
arv = arvore;
indice = ind;
}
public void run() {
System.out.println(threadName + "Trabalhando!!!");
pointerLock.lock();
try
{
while (arv.vizinhos[indice] != arv.vizinhos[arv.vizinhos[indice]])
{
//System.out.println(threadName + " Passou pelo while!!!");
arv.vizinhos[indice]= arv.vizinhos[arv.vizinhos[indice]];
//System.out.println(threadName + " Passou pela atribuicao!!!");
}
arv.imprime(indice);
}
finally
{
System.out.println(threadName + "Acabou!!!");
pointerLock.unlock();
}
}
}
[/code]
[code]package pointerJumping;
public class Tester {
public static void main(String[] args) throws InterruptedException {
//Criação da árvore para teste.
Arvore arv = new Arvore(5);
arv.setParent();
// Criação de objetos da classe Pointer_Jumping.
Pointer_Jumping pj1 = new Pointer_Jumping("thread 1",arv,0);
Pointer_Jumping pj2 = new Pointer_Jumping("thread 2",arv,1);
Pointer_Jumping pj3 = new Pointer_Jumping("thread 3",arv,2);
Pointer_Jumping pj4 = new Pointer_Jumping("thread 4",arv,3);
Pointer_Jumping pj5 = new Pointer_Jumping("thread 5",arv,4);
// Criação das Threads a partir de um objeto da classe Pointer_Jumping
Thread threadComRunnable1 = new Thread(pj1);
Thread threadComRunnable2 = new Thread(pj2);
Thread threadComRunnable3 = new Thread(pj3);
Thread threadComRunnable4 = new Thread(pj4);
Thread threadComRunnable5 = new Thread(pj5);
// Startando as Threads.
threadComRunnable1.start();
threadComRunnable2.start();
threadComRunnable3.start();
threadComRunnable4.start();
threadComRunnable5.start();
}
}
[/code]