Skip to content

Instantly share code, notes, and snippets.

@chochos
Created October 17, 2018 15:53
Show Gist options
  • Save chochos/1824c0db8ea12fbe8574e26576160090 to your computer and use it in GitHub Desktop.
Save chochos/1824c0db8ea12fbe8574e26576160090 to your computer and use it in GitHub Desktop.
Threads & Thread Pool Executors
public class PlainThreads {
public static void main(String... args) {
for (int i = 0; i < 100; i++) {
new Thread(new Task1()).start();
}
}
}
public class Task1 implements Runnable {
private final long created = System.currentTimeMillis();
@SneakyThrows
public void run() {
Thread.sleep(created%1000);
System.out.println("[" + Thread.currentThread().getName() + "] Slept " +
(System.currentTimeMillis()-created) + " millis");
}
}
public class ThreadPools {
private final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
private final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(16);
@SneakyThrows
public void run() {
//Encolar 100 tareas al cached
//Encolar 100 tareas al fixed
for (int i = 0; i < 100; i++) {
cachedThreadPool.execute(new Task1());
fixedThreadPool.execute(new Task1());
}
cachedThreadPool.shutdown();
fixedThreadPool.shutdown();
ThreadPoolExecutor tp1 = (ThreadPoolExecutor)cachedThreadPool;
ThreadPoolExecutor tp2 = (ThreadPoolExecutor)fixedThreadPool;
while (tp1.getCompletedTaskCount() < 100 || tp2.getCompletedTaskCount() < 100) {
System.out.println("CACHED " + tp1.getCompletedTaskCount() + " FIXED " + tp2.getCompletedTaskCount());
Thread.sleep(50);
}
System.out.println("Termina..?");
}
public static void main(String... args) {
new ThreadPools().run();
}
}
public class ThreadPoolsWithWrapper {
private final ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
private final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(16);
@SneakyThrows
public void run() {
//Encolar 100 tareas al cached
//Encolar 100 tareas al fixed
for (int i = 0; i < 100; i++) {
cachedThreadPool.execute(new Task1());
Wrapper w = new Wrapper(new Task1());
w.setQueued(System.currentTimeMillis());
try {
fixedThreadPool.execute(w);
} catch (RejectedExecutionException ex) {
w.run(); //o fail
}
}
cachedThreadPool.shutdown();
fixedThreadPool.shutdown();
ThreadPoolExecutor tp1 = (ThreadPoolExecutor)cachedThreadPool;
ThreadPoolExecutor tp2 = (ThreadPoolExecutor)fixedThreadPool;
while (tp1.getCompletedTaskCount() < 100 || tp2.getCompletedTaskCount() < 100) {
System.out.println("CACHED " + tp1.getCompletedTaskCount() + " FIXED " + tp2.getCompletedTaskCount());
Thread.sleep(50);
}
System.out.println("Termina..?");
}
public static void main(String... args) {
new ThreadPools().run();
}
}
@RequiredArgsConstructor
public class Wrapper implements Runnable {
@Setter @Getter
private static long timeout = 1000;
private final Runnable wrapped;
private final long created = System.currentTimeMillis();
@Setter @Getter
private long queued;
@Getter
private long executed;
@Getter
private long finished;
public void run() {
executed = System.currentTimeMillis();
if (executed - queued < timeout) {
wrapped.run();
} else {
System.out.println("Timeout! " + (executed-queued));
executed = -1;
}
finished = System.currentTimeMillis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment