This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new executor service factory impl
- Loading branch information
1 parent
0ec7b2f
commit d176694
Showing
3 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
pi4j-core/src/main/java/com/pi4j/concurrent/DefaultExecutorServiceFactory.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,106 @@ | ||
package com.pi4j.concurrent; | ||
|
||
/* | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : DefaultExecutorServiceFactory.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: http://www.pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2013 Pi4J | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
public class DefaultExecutorServiceFactory implements ExecutorServiceFactory { | ||
|
||
public static int MAX_THREADS_IN_POOL = 25; | ||
private static List<ExecutorService> singleThreadExecutorServices = new ArrayList<ExecutorService>(); | ||
private static ScheduledExecutorService scheduledExecutorService = null; | ||
private static ScheduledExecutorServiceWrapper executorServiceWrapper = null; | ||
|
||
/** | ||
* return an instance to the thread factory used to create new executor services | ||
*/ | ||
private ThreadFactory getThreadFactory(){ | ||
return Executors.defaultThreadFactory(); | ||
} | ||
|
||
/** | ||
* return an instance to the scheduled executor service (wrapper) | ||
*/ | ||
public ScheduledExecutorService getScheduledExecutorService(){ | ||
if(scheduledExecutorService == null){ | ||
scheduledExecutorService = Executors.newScheduledThreadPool(MAX_THREADS_IN_POOL, getThreadFactory()); | ||
executorServiceWrapper = new ScheduledExecutorServiceWrapper(scheduledExecutorService); | ||
} | ||
|
||
// we return the protected wrapper to prevent any consumers from | ||
// being able to shutdown the scheduled executor service | ||
return executorServiceWrapper; | ||
} | ||
|
||
/** | ||
* return a new instance of a single thread executor service | ||
*/ | ||
public ExecutorService newSingleThreadExecutorService(){ | ||
|
||
// create new single thread executor | ||
ExecutorService singleThreadExecutorService = Executors.newSingleThreadExecutor(getThreadFactory()); | ||
|
||
// add new instance to managed collection | ||
singleThreadExecutorServices.add(singleThreadExecutorService); | ||
|
||
// return the new instance | ||
return singleThreadExecutorService; | ||
} | ||
|
||
/** | ||
* shutdown executor threads | ||
*/ | ||
public void shutdown(){ | ||
|
||
// shutdown each single thread executor in the managed collection | ||
for(ExecutorService singleThreadExecutorService : singleThreadExecutorServices){ | ||
if(singleThreadExecutorService != null){ | ||
if(!singleThreadExecutorService.isShutdown()){ | ||
// this is a forceful shutdown; | ||
// don't wait for the active tasks to complete | ||
singleThreadExecutorService.shutdownNow(); | ||
} | ||
} | ||
} | ||
|
||
// shutdown scheduled executor instance | ||
if(scheduledExecutorService != null){ | ||
if(!scheduledExecutorService.isShutdown()){ | ||
// this is a forceful shutdown; | ||
// don't wait for the scheduled tasks to complete | ||
scheduledExecutorService.shutdownNow(); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
pi4j-core/src/main/java/com/pi4j/concurrent/ExecutorServiceFactory.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,39 @@ | ||
package com.pi4j.concurrent; | ||
|
||
/* | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : ExecutorServiceFactory.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: http://www.pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2013 Pi4J | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
public interface ExecutorServiceFactory | ||
{ | ||
public ScheduledExecutorService getScheduledExecutorService(); | ||
public ExecutorService newSingleThreadExecutorService(); | ||
public void shutdown(); | ||
} |
141 changes: 141 additions & 0 deletions
141
pi4j-core/src/main/java/com/pi4j/concurrent/ScheduledExecutorServiceWrapper.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,141 @@ | ||
package com.pi4j.concurrent; | ||
|
||
/* | ||
* #%L | ||
* ********************************************************************** | ||
* ORGANIZATION : Pi4J | ||
* PROJECT : Pi4J :: Java Library (Core) | ||
* FILENAME : ScheduledExecutorServiceWrapper.java | ||
* | ||
* This file is part of the Pi4J project. More information about | ||
* this project can be found here: http://www.pi4j.com/ | ||
* ********************************************************************** | ||
* %% | ||
* Copyright (C) 2012 - 2013 Pi4J | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public class ScheduledExecutorServiceWrapper implements ScheduledExecutorService { | ||
|
||
private ScheduledExecutorService service; | ||
|
||
/** | ||
* Default constructor | ||
* @param service | ||
*/ | ||
public ScheduledExecutorServiceWrapper(ScheduledExecutorService service) { | ||
this.service = service; | ||
} | ||
|
||
@Override | ||
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { | ||
return service.awaitTermination(timeout, unit); | ||
} | ||
|
||
@Override | ||
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException { | ||
return service.invokeAll(tasks); | ||
} | ||
|
||
@Override | ||
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) | ||
throws InterruptedException { | ||
return service.invokeAll(tasks, timeout, unit); | ||
} | ||
|
||
@Override | ||
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { | ||
return service.invokeAny(tasks); | ||
} | ||
|
||
@Override | ||
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) | ||
throws InterruptedException, ExecutionException, TimeoutException { | ||
return service.invokeAny(tasks, timeout, unit); | ||
} | ||
|
||
@Override | ||
public boolean isShutdown() { | ||
return service.isShutdown(); | ||
} | ||
|
||
@Override | ||
public boolean isTerminated() { | ||
return service.isShutdown(); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
throw new UnsupportedOperationException("This scheduled executor service can only be shutdown by Pi4J."); | ||
} | ||
|
||
@Override | ||
public List<Runnable> shutdownNow() { | ||
throw new UnsupportedOperationException("This scheduled executor service can only be shutdown by Pi4J."); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Callable<T> task) { | ||
return service.submit(task); | ||
} | ||
|
||
@Override | ||
public Future<?> submit(Runnable task) { | ||
return service.submit(task); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Runnable task, T result) { | ||
return service.submit(task, result); | ||
} | ||
|
||
@Override | ||
public void execute(Runnable command) { | ||
service.execute(command); | ||
} | ||
|
||
@Override | ||
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { | ||
return service.schedule(command, delay, unit); | ||
} | ||
|
||
@Override | ||
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { | ||
return service.schedule(callable, delay, unit); | ||
} | ||
|
||
@Override | ||
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, | ||
TimeUnit unit) { | ||
return service.scheduleAtFixedRate(command, initialDelay, period, unit); | ||
} | ||
|
||
@Override | ||
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, | ||
long delay, TimeUnit unit) { | ||
return service.scheduleWithFixedDelay(command, initialDelay, delay, unit); | ||
} | ||
} |