“Ejecutores Java” Código de respuesta

Ejecutores Java

package pg01.c04;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
	public static void main(String[] args) {
		//ExecutorService executor = Executors.newFixedThreadPool(1); //Creates 1 thread in the pool for executting all
		//ExecutorService executor = Executors.newFixedThreadPool(2); //Creates 2 threads in the pool for executting all
		//ExecutorService executor = Executors.newFixedThreadPool(3); //Creates 3 threads in the pool for executting all
		//ExecutorService executor = Executors.newSingleThreadExecutor(); //Cretaes 1 single thread for the execution
		ExecutorService executor = Executors.newCachedThreadPool(); //Cretaes as many threads as it needs and reuses the ones created 
		//creates the logic threads but it doesn´t mean that its going to use 3 threads in the execution
		Thread hilox = new Thread(new MyThread("X",100, 100));
		Thread hiloO = new Thread(new MyThread("O",100, 100));
		Thread hilo_ = new Thread(new MyThread("_",100, 100));
		//Executes the threads
		executor.execute(hilo_); 
		executor.execute(hiloO);
		executor.execute(hilox);
		// closes the threads because if not they will remain waiting although the execution finished
		executor.shutdown();
		
		

	}
}
public class MyThread implements Runnable{
	private String s;
	private int l;
	private int r;
	public MyThread (String s, int l,int r) {
		this.s= s;
		this.l= l;
		this.r= r;
	}
	public void run() {
		// TODO Auto-generated method stub
		for(int i =0; i<r;i++) {
			System.out.print(s);
			for (int j=0;j<10000;j++) {
				System.out.print("*");
			}
			try {
				Thread.sleep(l);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println();
	}
}
Smoggy Sardine

Ejecutores Java

ExecutorService executor = Executors.newFixedThreadPool(10);
Dangerous Dolphin

Respuestas similares a “Ejecutores Java”

Preguntas similares a “Ejecutores Java”

Más respuestas relacionadas con “Ejecutores Java” en Java

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código