forked from Leaking/Hunter
-
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.
- Loading branch information
weikaiyun
committed
Dec 2, 2020
1 parent
f06a959
commit 5361b67
Showing
4 changed files
with
103 additions
and
11 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
29 changes: 29 additions & 0 deletions
29
hunter-transform/src/main/java/com/quinn/hunter/transform/concurrent/Schedulers.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,29 @@ | ||
package com.quinn.hunter.transform.concurrent; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Schedulers { | ||
private static final int cpuCount = Runtime.getRuntime().availableProcessors(); | ||
private final static ExecutorService IO = new ThreadPoolExecutor(0, cpuCount * 3, | ||
30L, TimeUnit.SECONDS, | ||
new LinkedBlockingQueue<>()); | ||
|
||
private static final ExecutorService COMPUTATION = Executors.newWorkStealingPool(cpuCount); | ||
|
||
public static Worker IO() { | ||
return new Worker(IO); | ||
} | ||
|
||
public static Worker COMPUTATION() { | ||
return new Worker(COMPUTATION); | ||
} | ||
|
||
public static ForkJoinPool FORKJOINPOOL() { | ||
return (ForkJoinPool) COMPUTATION; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
hunter-transform/src/main/java/com/quinn/hunter/transform/concurrent/Worker.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,62 @@ | ||
package com.quinn.hunter.transform.concurrent; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Future; | ||
import java.util.function.Consumer; | ||
|
||
public class Worker { | ||
protected final LinkedList<Future<?>> futures = new LinkedList<Future<?>>() { | ||
@Override | ||
public synchronized boolean add(Future<?> future) { | ||
return super.add(future); | ||
} | ||
|
||
@Override | ||
public synchronized Future<?> pollFirst() { | ||
return super.pollFirst(); | ||
} | ||
}; | ||
protected ExecutorService executor; | ||
|
||
Worker(ExecutorService executor) { | ||
this.executor = executor; | ||
} | ||
|
||
public void execute(Runnable runnable) { | ||
futures.add(executor.submit(runnable)); | ||
} | ||
|
||
public <T> Future<T> submit(Callable<T> callable) { | ||
Future<T> future = executor.submit(callable); | ||
futures.add(future); | ||
return future; | ||
} | ||
|
||
public void await() throws IOException { | ||
Future<?> future; | ||
while ((future = futures.pollFirst()) != null) { | ||
try { | ||
future.get(); | ||
} catch (ExecutionException | InterruptedException e) { | ||
if (e.getCause() instanceof IOException) { | ||
throw (IOException) e.getCause(); | ||
} else if (e.getCause() instanceof RuntimeException) { | ||
throw (RuntimeException) e.getCause(); | ||
} else if (e.getCause() instanceof Error) { | ||
throw (Error) e.getCause(); | ||
} | ||
throw new RuntimeException(e.getCause()); | ||
} | ||
} | ||
} | ||
|
||
public <I> void submitAndAwait(Collection<I> is, Consumer<I> consumer) throws IOException { | ||
is.stream().map(f -> (Runnable) () -> consumer.accept(f)).forEach(this::execute); | ||
await(); | ||
} | ||
} |