Created
November 16, 2019 13:02
-
-
Save tranch/d44d8c141e8cf85f5434c8bc40b83d23 to your computer and use it in GitHub Desktop.
This file contains 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 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