Created
April 15, 2016 18:01
-
-
Save southerton81/96e141b8feede3fe0b8f88f679bef381 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 javaapplication1; | |
/** | |
* Test java ThreadPoolExecutor can shrink to guarantee only one thread is executing at a given | |
* time, and then restore thread pool size. | |
*/ | |
import java.util.concurrent.BlockingQueue; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.ThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
class TestRunnable implements Runnable { | |
int num; | |
long sleep; | |
CountDownLatch waitForStart; | |
CountDownLatch waitForEnd; | |
TestRunnable(CountDownLatch cdl, CountDownLatch cdl2, int num, long sleep) { | |
this.num = num; | |
this.sleep = sleep; | |
this.waitForStart = cdl; | |
this.waitForEnd = cdl2; | |
} | |
@Override | |
public void run() { | |
waitForStart.countDown(); | |
System.out.println("test thread " + num + " enter"); | |
try { | |
Thread.sleep(sleep); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("test thread " + num + " exit"); | |
waitForEnd.countDown(); | |
} | |
} | |
public class JavaApplication1 { | |
private static final BlockingQueue<Runnable> sPoolWorkQueue | |
= new LinkedBlockingQueue<Runnable>(128); | |
public static void main(String[] args) { | |
ThreadPoolExecutor tp = (ThreadPoolExecutor) new ThreadPoolExecutor(8, 14, 60, | |
TimeUnit.SECONDS, sPoolWorkQueue); | |
CountDownLatch waitForEnd = new CountDownLatch(1); | |
CountDownLatch waitForStart = new CountDownLatch(4); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 1, 1000)); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 2, 2000)); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 3, 3000)); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 4, 4000)); | |
try { | |
waitForStart.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
long gg = tp.getKeepAliveTime(TimeUnit.MILLISECONDS); | |
tp.setCorePoolSize(1); | |
tp.setMaximumPoolSize(1); | |
CountDownLatch waitForEnd2 = new CountDownLatch(1); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd2, 50, 1000)); | |
try { | |
waitForEnd2.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
// Restore | |
tp.setCorePoolSize(8); | |
tp.setMaximumPoolSize(14); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 1, 1000)); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 2, 2000)); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 3, 3000)); | |
tp.execute(new TestRunnable(waitForStart, waitForEnd, 4, 4000)); | |
try { | |
tp.awaitTermination(60, TimeUnit.SECONDS); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment