-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
1,704 additions
and
217 deletions.
There are no files selected for viewing
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
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
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
54 changes: 54 additions & 0 deletions
54
common/src/main/java/com/alibaba/fescar/common/thread/RejectedPolicies.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,54 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.fescar.common.thread; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.RejectedExecutionHandler; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
/** | ||
* Policies for RejectedExecutionHandler | ||
* | ||
* Created by guoyao on 2019/2/26. | ||
*/ | ||
public final class RejectedPolicies { | ||
|
||
/** | ||
* when rejected happened ,add the new task and run the oldest task | ||
* | ||
* @return rejected execution handler | ||
*/ | ||
public static RejectedExecutionHandler runsOldestTaskPolicy() { | ||
return new RejectedExecutionHandler() { | ||
@Override | ||
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { | ||
if (executor.isShutdown()) { | ||
return; | ||
} | ||
BlockingQueue<Runnable> workQueue = executor.getQueue(); | ||
Runnable firstWork = workQueue.poll(); | ||
boolean newTaskAdd = workQueue.offer(r); | ||
if (firstWork != null) { | ||
firstWork.run(); | ||
} | ||
if (!newTaskAdd) { | ||
executor.execute(r); | ||
} | ||
} | ||
}; | ||
} | ||
} |
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
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
97 changes: 97 additions & 0 deletions
97
common/src/test/java/com/alibaba/fescar/common/thread/RejectedPoliciesTest.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,97 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.fescar.common.thread; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Created by guoyao on 2019/2/26. | ||
*/ | ||
public class RejectedPoliciesTest { | ||
|
||
private final int DEFAULT_CORE_POOL_SIZE = 1; | ||
private final int DEFAULT_KEEP_ALIVE_TIME = 10; | ||
private final int MAX_QUEUE_SIZE = 1; | ||
|
||
/** | ||
* Test runs oldest task policy. | ||
* | ||
* @throws Exception the exception | ||
*/ | ||
@Test | ||
public void testRunsOldestTaskPolicy() throws Exception { | ||
AtomicInteger atomicInteger = new AtomicInteger(); | ||
ThreadPoolExecutor poolExecutor = | ||
new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_CORE_POOL_SIZE, DEFAULT_KEEP_ALIVE_TIME, | ||
TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue(MAX_QUEUE_SIZE), | ||
new NamedThreadFactory("OldestRunsPolicy", DEFAULT_CORE_POOL_SIZE), | ||
RejectedPolicies.runsOldestTaskPolicy()); | ||
CountDownLatch downLatch1 = new CountDownLatch(1); | ||
CountDownLatch downLatch2 = new CountDownLatch(1); | ||
CountDownLatch downLatch3 = new CountDownLatch(1); | ||
//task1 | ||
poolExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
//wait the oldest task of queue count down | ||
downLatch1.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
atomicInteger.getAndAdd(1); | ||
} | ||
}); | ||
assertThat(atomicInteger.get()).isEqualTo(0); | ||
//task2 | ||
poolExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
// run second | ||
atomicInteger.getAndAdd(2); | ||
} | ||
}); | ||
//task3 | ||
poolExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
downLatch2.countDown(); | ||
//task3 run | ||
atomicInteger.getAndAdd(3); | ||
downLatch3.countDown(); | ||
} | ||
}); | ||
//only the task2 run which is the oldest task of queue | ||
assertThat(atomicInteger.get()).isEqualTo(2); | ||
downLatch1.countDown(); | ||
downLatch2.await(); | ||
//wait task3 run +3 | ||
downLatch3.await(); | ||
//run task3 | ||
assertThat(atomicInteger.get()).isEqualTo(6); | ||
|
||
} | ||
} |
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
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
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
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
Oops, something went wrong.