Last active
March 13, 2017 20:38
-
-
Save vladimirdolzhenko/036f5648b5cd93ca525cfd64a0734b3c 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; | |
/** | |
$ uname -a | |
Darwin Vladimirs-MacBook-Pro.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64 | |
$ java -version | |
java version "1.8.0_112" | |
Java(TM) SE Runtime Environment (build 1.8.0_112-b16) | |
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode) | |
// the same results on java 9, and java 10 | |
$ java com.TPEM | |
main submit:0 | |
main submit:1 | |
main submit:2 | |
pool-1-thread-1 start:0 | |
Exception on submitting 2: Task com.TPEM$1@4b1210ee rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:3 | |
Exception on submitting 3: Task com.TPEM$1@3cd1a2f1 rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:4 | |
Exception on submitting 4: Task com.TPEM$1@2f0e140b rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:5 | |
Exception on submitting 5: Task com.TPEM$1@7440e464 rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:6 | |
Exception on submitting 6: Task com.TPEM$1@49476842 rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:7 | |
Exception on submitting 7: Task com.TPEM$1@78308db1 rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:8 | |
Exception on submitting 8: Task com.TPEM$1@27c170f0 rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
main submit:9 | |
Exception on submitting 9: Task com.TPEM$1@5451c3a8 rejected from java.util.concurrent.ThreadPoolExecutor@4d7e1886[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0] | |
*/ | |
import java.util.concurrent.ArrayBlockingQueue; | |
import java.util.concurrent.ThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* @author [email protected] | |
* @since 2017-03-13 | |
*/ | |
public class TPEM { | |
public static void main(String[] args) { | |
ThreadPoolExecutor threadPoolExecutor = | |
new ThreadPoolExecutor(1, | |
1, | |
60, | |
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), | |
new ThreadPoolExecutor.AbortPolicy()); | |
for(int i = 0; i < 10; i++) { | |
int task = i; | |
System.out.println(Thread.currentThread().getName() + " submit:" + task); | |
try { | |
threadPoolExecutor.execute(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
System.out.println(Thread.currentThread().getName() + " start:" + task); | |
Thread.sleep(20_000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(Thread.currentThread().getName() + " end:" + task); | |
} | |
}); | |
} catch (Exception e){ | |
System.out.println("Exception on submitting " + task + ": " + e.getMessage()); | |
} | |
} | |
threadPoolExecutor.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment