Escolar Documentos
Profissional Documentos
Cultura Documentos
Programao Concorrente
public Pedido(int n) {
this.n = n;}
// mtodo get pega e retorna o n
public int getNumero() {
return n;}
// mtodo que seta o valor de n
public void setNumero(int n) {
this.n = n }
}
//classe buffer com quantidade de vetor especifico
Public class BufferLimitado {
//declarao de vetor do buffer
private Pedido pedidos[] = new Pedido[5000];
private int Escrita = 0;
private int Leitura = 0;
//mtodo com looping para o buffer
private void inicioBufferLimitado5000(){
for (int i = 0; i < 5000; i++) {
addPedido(new Pedido(i));
}
}
public void addPedido(Pedido pedido) {
pedidos[Escrita] = pedido;
Escrita++;
}
// condio para a leitura e escrita com o retorno
public Pedido getPedido() {
if (Leitura <= Escrita) {
return pedidos[Leitura];
}
return null;
}
}
Import java.text.SimpleDateFormat;
//classe log para o preenchimento e identificao de trhead
public class Log {
// variveis do log
private SimpleDateFormat date = new SimpleDateFormat("hh:MM:ss.S");
private long inicio;
private long init;
private int pedido;
public Log(long inicio, long init, int pedido) {
this.inicio = inicio;
this.init = init;
this.pedido = pedido;
}
@Override
public String toString() {
return String.format("comeo %s final %s soma %d pedido %d",
date.format(inicio),
date.format(final), (final - inicio), pedido);
}
}
Import java.text.SimpleDateFormat;
//classe que possui o mtodo main para execuo
public class Executa {
public static void main(String[] args) {
Log logs[] = new Log[5000];
long inicio = System.currentTimeMillis();
//metodo try e catch para parada e interrupo de execeo
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(Executa.class.getName())
.log(Level.SEVERE, null, ex);
}
long final = System.currentTimeMillis();
Log log = new Log(inicio, init, 5000);
System.out.println(log);
}
}
// execuo 1 trhead
public class thread1 extends Thread {
private String mensagem;
private int tempo;
public thread1(String mensagem, int tempo) {
this.mensagem = mensagem;
this.tempo = tempo;
}
public void run() {
for (int i = 0; i < 10; i++)
{
try {
}
catch(Exception e) {
}
System.out.println(mensagem + ", contando: " + i);
}
}
}
public class Multiplasthreds {
public static void main(String[] args) {
thread1 c1 = new thread1("thread", 1000);
c1.start();try {
}catch (Exception e) {
}System.out.println("Fim.")
}
}
//Execuo 10 trheads
public class thread10 extends Thread {
private String mensagem;
private int tempo;
public thread10(String mensagem, int tempo) {
this.mensagem = mensagem;
this.tempo = tempo;
}
public void run() {
for (int i = 0; i < 10; i++)
{ try {
}catch(Exception e) {
}
System.out.println(mensagem + ", contando: " + i);
}
}
}public class Multiplasthreads {
public static void main(String[] args) {
for (cont = 0;cont <10;cont++) {
thread10c[cont] = new thread10("thread [cont]", 1000);
C[cont].start();
}try {
}catch (Exception e) {
}System.out.println("Fim.")
}
}
//Execuo 50 threads
public class thread50 extends Thread {
private String mensagem;
private int tempo;
public thread50(String mensagem, int tempo) {
this.mensagem = mensagem;
this.tempo = tempo;
}
public void run() {
for (int i = 0; i < 10; i++)
{ try {
}catch(Exception e) {
}System.out.println(mensagem + ", contando: " + i);
}
}
}
public class Multiplasthread {
public static void main(String[] args) {
for (cont = 0;cont <50;cont++) {
thread50 c[cont] = new thread50("thread [cont]", 1000);
C[cont].start();
}try {
}catch (Exception e) {
}System.out.println("Fim.")
{{
}System.out.println("Fim.");
}
}
//Execuo 1000 threads
public class thread1000 extends Thread {
private String mensagem;
private int tempo;
public thread1000(String mensagem, int tempo) {
this.mensagem = mensagem;
this.tempo = tempo;
}
public void run() {
for (int i = 0; i < 10; i++){
try {
}catch(Exception e) {
}
System.out.println(mensagem + ", contando: " + i);
}
}
}
public class Multiplasthreads {
public static void main(String[] args) {
for (cont = 0;cont <1000;cont++) {
thread1000 c[cont] = new thread1000 ("thread[cont]", 1000);
C[cont].start();
}try {
}catch (Exception e) {
}
System.out.println("Fim.");}
|
11:09:43.111
11:10:18.308
11:10:13.769
11:10:48.651
11:10:59.285
|
|
|
|
|
11:09:44.111
11:10:27.308
11:10:22.769
11:10:57.651
11:10:08.285
|
|
|
|
|
quant.pedido |
1 | 10 | 10 | 9 | 12 | 10 | 10 |
10 | 1285 | 1325 | 1366 | 1384 | 1623 | 1264 |
50 | 1573 | 1459 | 1667 | 1562 | 1306 | 1475 |
500 | 124753 | 123692 | 118913 | 110266 | 128742 | 128741 |
1000 | 512022 | 521478 | 512658 | 526854 | 535285 | 514748 |
@Override
public void run() {
int num = 0;
for (int count = 1; count <= 5000; count++ )
{
try
{
Thread.sleep(generator.nextInt( 3000 ));
num += sharedLocation.get();
System.out.printf("\t\t\t%d\n", num);
}
catch( InterruptedException exception )
{
exception.printStackTrace();
}
} //fim for
System.out.printf("\n%s %d.\n%s\n", "Total lido pelo consumidor", sum,
"Consumidor acbou.");
public class Producer implements Runnable {
private static Random generator = new Random();