Skip to content

Commit

Permalink
Shutdown executors, fix applet example, fixes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 21, 2013
1 parent 696bf68 commit 94859e8
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 49 deletions.
7 changes: 0 additions & 7 deletions webcam-capture-drivers/webcam-capture-driver-v4l4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
1 change: 1 addition & 0 deletions webcam-capture-examples/webcam-capture-applet/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</classpathentry>
<classpathentry kind="src" path="src/main/html"/>
<classpathentry kind="src" path="src/etc/resources"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
1 change: 1 addition & 0 deletions webcam-capture-examples/webcam-capture-applet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</dependencies>

<build>
<finalName>applet</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
</head>
<body>
<p>This is my page, below you see an Webcam Capture applet</p>
<applet codebase="."
archive="${project.artifactId}-${project.version}.jar"
code="WebcamAppletExample.class"
width="176" height="144" alt="Applet">
<applet codebase="." archive="applet.jar" code="WebcamAppletExample.class" width="320" height="240" alt="Webcam Capture Applet">
<param name="separate_jvm" value="true" />
</applet>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver;


public class WebcamAppletExample extends JApplet {

private static final long serialVersionUID = 3517366452510566924L;

private Dimension size = WebcamResolution.QQVGA.getSize();
private Dimension size = WebcamResolution.QVGA.getSize();
private Webcam webcam = null;
private WebcamPanel panel = null;

Expand All @@ -28,12 +27,6 @@ public void start() {

super.start();

try {
Webcam.setDriver(new WebcamDefaultDriver());
} catch (Exception e) {
e.printStackTrace();
}

webcam = Webcam.getDefault();
webcam.setViewSize(size);

Expand Down Expand Up @@ -67,23 +60,20 @@ public void start() {
@Override
public void destroy() {
System.out.println("Destroy");
super.destroy();
webcam.close();
webcam.close();
Webcam.shutdown();
System.out.println("Destroyed");
}

@Override
public void stop() {
System.out.println("Stop");
super.stop();
webcam.close();
webcam.getLock().unlock();
panel.stop();
System.out.println("Stopped");
}

@Override
public void init() {
System.out.println("Init");
super.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configuration scan="true" scanPeriod="30 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
32 changes: 22 additions & 10 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ public boolean close() {

if (open.compareAndSet(true, false)) {

LOG.debug("Closing webcam {}", getName());

assert updater != null;
assert lock != null;

Expand All @@ -258,20 +260,15 @@ public boolean close() {
throw e;
}

// unlock webcam so other Java processes can start using it

lock.unlock();

// stop updater

if (asynchronous) {
updater.stop();
}
updater.stop();

// remove shutdown hook (it's not more necessary)

removeShutdownHook();

// unlock webcam so other Java processes can start using it
lock.unlock();

// notify listeners

WebcamEvent we = new WebcamEvent(WebcamEventType.CLOSED, this);
Expand All @@ -287,8 +284,10 @@ public boolean close() {
}
}

LOG.debug("Webcam {} has been closed", getName());

} else {
LOG.debug("Webcam is already closed {}", getName());
LOG.debug("Webcam {} is already closed", getName());
}

return true;
Expand Down Expand Up @@ -1086,4 +1085,17 @@ public void setImageTransformer(WebcamImageTransformer transformer) {
public WebcamLock getLock() {
return lock;
}

public static void shutdown() {

// stop discovery service
WebcamDiscoveryService discovery = getDiscoveryServiceRef();
if (discovery != null) {
discovery.stop();
}

// stop processor
WebcamProcessor.getInstance().shutdown();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ public void run() {
try {
monitor.wait(support.getScanInterval());
} catch (InterruptedException e) {
if (LOG.isTraceEnabled()) {
LOG.error("Interrupted", e);
}
break;
} catch (Exception e) {
throw new RuntimeException("Problem waiting on monitor", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WebcamProcessor {

private static final Logger LOG = LoggerFactory.getLogger(WebcamProcessor.class);

/**
* Thread factory for processor.
*
Expand Down Expand Up @@ -49,7 +55,6 @@ private static final class AtomicProcessor implements Runnable {
* @throws InterruptedException when thread has been interrupted
*/
public void process(WebcamTask task) throws InterruptedException {

inbound.put(task);

Throwable t = outbound.take().getThrowable();
Expand Down Expand Up @@ -93,7 +98,7 @@ public void run() {
/**
* Execution service.
*/
private static final ExecutorService runner = Executors.newSingleThreadExecutor(new ProcessorThreadFactory());
private static ExecutorService runner = null;

/**
* Static processor.
Expand All @@ -115,16 +120,44 @@ private WebcamProcessor() {
* @throws InterruptedException when thread has been interrupted
*/
public void process(WebcamTask task) throws InterruptedException {

if (started.compareAndSet(false, true)) {
runner = Executors.newSingleThreadExecutor(new ProcessorThreadFactory());
runner.execute(processor);
}

if (!runner.isShutdown()) {
processor.process(task);
} else {
throw new RejectedExecutionException("Cannot process because processor runner has been already shut down");
}
}

public void shutdown() {
if (started.compareAndSet(true, false)) {

LOG.debug("Shutting down webcam processor");

runner.shutdown();

LOG.debug("Awaiting tasks termination");

while (runner.isTerminated()) {

try {
runner.awaitTermination(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
return;
}

runner.shutdownNow();
}

LOG.debug("All tasks has been terminated");
}

}

public static synchronized WebcamProcessor getInstance() {
return INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -102,7 +103,7 @@ public void run() {
/**
* Executor service.
*/
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY);
private ScheduledExecutorService executor = null;

/**
* Executor service for image notifications.
Expand All @@ -127,7 +128,7 @@ public void run() {
/**
* Is updater running.
*/
private volatile boolean running = false;
private AtomicBoolean running = new AtomicBoolean(false);

private volatile boolean imageNew = false;

Expand All @@ -144,25 +145,46 @@ protected WebcamUpdater(Webcam webcam) {
* Start updater.
*/
public void start() {
running = true;
image.set(new WebcamReadImageTask(Webcam.getDriver(), webcam.getDevice()).getImage());
executor.execute(this);
if (running.compareAndSet(false, true)) {

LOG.debug("Webcam updater has been started");
image.set(new WebcamReadImageTask(Webcam.getDriver(), webcam.getDevice()).getImage());

executor = Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY);
executor.execute(this);

LOG.debug("Webcam updater has been started");
} else {
LOG.debug("Webcam updater is already started");
}
}

/**
* Stop updater.
*/
public void stop() {
running = false;
LOG.debug("Webcam updater has been stopped");
if (running.compareAndSet(true, false)) {

executor.shutdown();

while (!executor.isTerminated()) {
try {
executor.awaitTermination(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOG.trace(e.getMessage(), e);
return;
}
}

LOG.debug("Webcam updater has been stopped");
} else {
LOG.debug("Webcam updater is already stopped");
}
}

@Override
public void run() {

if (!running) {
if (!running.get()) {
return;
}

Expand Down

0 comments on commit 94859e8

Please sign in to comment.