Skip to content

Instantly share code, notes, and snippets.

@tranch
Created November 16, 2019 13:02
Show Gist options
  • Save tranch/d44d8c141e8cf85f5434c8bc40b83d23 to your computer and use it in GitHub Desktop.
Save tranch/d44d8c141e8cf85f5434c8bc40b83d23 to your computer and use it in GitHub Desktop.
package com.honey;
import java.util.concurrent.*;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(10);
ExecutorService pool = new ThreadPoolExecutor(2, 2, 2, TimeUnit.SECONDS, blockingQueue, new ThreadPoolExecutor.CallerRunsPolicy());
int count = 20;
for (int i = 0; i < count; i++) {
System.out.println("for "+ i +" start->"+System.currentTimeMillis());
pool.submit(new AppRunnable(i));
System.out.println("for " + i + " end ->"+System.currentTimeMillis());
}
pool.shutdown();
}
static class AppRunnable implements Runnable {
final int i;
AppRunnable(int i) {
this.i = i;
}
@Override
public void run() {
System.out.println(i+"->" + Thread.currentThread().getName());
try {
Thread.sleep(20L);
} catch (InterruptedException e) {
//ignore
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment