Created
July 10, 2013 18:00
-
-
Save tonussi/5968585 to your computer and use it in GitHub Desktop.
Prioridades em Java Threads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tonussi.concorrente.prioridades; | |
public class Indice { | |
int i, limite; | |
public Indice(int limite) { | |
i = 0; | |
this.limite = limite; | |
} | |
public synchronized int get() { | |
int retorno; | |
if (i < limite) { | |
retorno = i; | |
i++; | |
} else | |
retorno = -99999; | |
return (retorno); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tonussi.concorrente.prioridades; | |
public class Main { | |
public static void main(String[] s) { | |
int i; | |
int cont[] = new int[5]; | |
int vetor[] = new int[1000]; | |
Indice indice = new Indice(1000); | |
Processo proc1, proc2, proc3, proc4; | |
//Criando as Threads | |
proc1 = new Processo(1, indice, vetor); | |
proc2 = new Processo(2, indice, vetor); | |
proc3 = new Processo(3, indice, vetor); | |
proc4 = new Processo(4, indice, vetor); | |
//Atribuindo prioridades | |
proc1.setPriority(5); | |
proc2.setPriority(5); | |
proc3.setPriority(5); | |
proc4.setPriority(5); | |
//Inicializando as threads | |
proc1.start(); | |
proc2.start(); | |
proc3.start(); | |
proc4.start(); | |
//aguardando a finalização das threads | |
try { | |
proc1.join(); | |
proc2.join(); | |
proc3.join(); | |
proc4.join(); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
for (i = 0; i < 1000; i++) | |
cont[vetor[i]]++; | |
System.out.println("Numero de 1: " + cont[1]); | |
System.out.println("Numero de 2: " + cont[2]); | |
System.out.println("Numero de 3: " + cont[3]); | |
System.out.println("Numero de 4: " + cont[4]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tonussi.concorrente.prioridades; | |
public class Processo extends Thread { | |
int pid, i; | |
Indice indice; | |
int vetor[]; | |
public Processo() { | |
super(); | |
} | |
public Processo(int pid, Indice indice, int[] vetor) { | |
this.pid = pid; | |
this.indice = indice; | |
this.vetor = vetor; | |
} | |
public void run() { | |
while (i >= 0) { | |
i = indice.get(); | |
if (i >= 0) { | |
vetor[i] = pid; | |
for (int k = 1; k < 500; k++) | |
for (int l = 1; l < 5000; l++); | |
} | |
} | |
} | |
public int getPid() { | |
return pid; | |
} | |
public void setPid(int pid) { | |
this.pid = pid; | |
} | |
public int getI() { | |
return i; | |
} | |
public void setI(int i) { | |
this.i = i; | |
} | |
public Indice getIndice() { | |
return indice; | |
} | |
public void setIndice(Indice indice) { | |
this.indice = indice; | |
} | |
public int[] getVetor() { | |
return vetor; | |
} | |
public void setVetor(int[] vetor) { | |
this.vetor = vetor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment