forked from alibaba/transmittable-thread-local
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add demo code
CustomizedBlockingQueueWithTtlDemo
for alibaba#340
- Loading branch information
1 parent
c3e04b7
commit ed486fb
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/test/java/com/alibaba/demo/ttl/CustomizedBlockingQueueWithTtlDemo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.alibaba.demo.ttl; | ||
|
||
import com.alibaba.ttl.TtlRunnable; | ||
import com.alibaba.ttl.threadpool.TtlExecutors; | ||
|
||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Simple demo code for issue | ||
* https://github.com/alibaba/transmittable-thread-local/issues/340 | ||
*/ | ||
public class CustomizedBlockingQueueWithTtlDemo { | ||
public static void main(String[] args) throws Exception { | ||
final MyBlockingQueue myBlockingQueue = new MyBlockingQueue(); | ||
|
||
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor( | ||
1, 1, 1L, TimeUnit.SECONDS, myBlockingQueue); | ||
final ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(threadPoolExecutor); | ||
|
||
ttlExecutorService.execute(new MyTask("accept-1")); | ||
ttlExecutorService.execute(new MyTask("DISCARDED")); | ||
ttlExecutorService.execute(new MyTask("accept-2")); | ||
|
||
ttlExecutorService.shutdown(); | ||
if (!ttlExecutorService.awaitTermination(10, TimeUnit.SECONDS)) { | ||
throw new IllegalStateException("Fail to shutdown executor service"); | ||
} | ||
} | ||
|
||
private static class MyTask implements Runnable { | ||
private final String msg; | ||
|
||
private MyTask(String msg) { | ||
this.msg = msg; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
System.out.println("MyTask: " + msg); | ||
} | ||
} | ||
|
||
private static class MyBlockingQueue extends ArrayBlockingQueue<Runnable> { | ||
public MyBlockingQueue() { | ||
super(16); | ||
} | ||
|
||
@Override | ||
public boolean offer(final Runnable runnable) { | ||
// unwrap TtlRunnable first | ||
final Runnable unwrap = TtlRunnable.unwrap(runnable); | ||
|
||
final MyTask myTask = (MyTask) unwrap; | ||
if (myTask.msg.startsWith("accept-")) { | ||
// ignore result/return value of offer, BAD!! | ||
// does not follow the contract of method offer | ||
// this is just a simple demo! | ||
super.offer(runnable); | ||
} | ||
|
||
// always return true even if discard the task, BAD!! | ||
// does not follow the contract of method offer | ||
// this is just a simple demo! | ||
return true; | ||
} | ||
|
||
@Override | ||
public void put(final Runnable runnable) throws InterruptedException { | ||
// unwrap TtlRunnable first | ||
final Runnable unwrap = TtlRunnable.unwrap(runnable); | ||
|
||
final MyTask myTask = (MyTask) unwrap; | ||
// discard task if not satisfied, BAD!! | ||
// does not follow the contract of method put | ||
// this is just a simple demo! | ||
if (myTask.msg.startsWith("accept-")) { | ||
super.put(runnable); | ||
} | ||
} | ||
} | ||
} | ||
|