我对线程的同步知之甚少......我正在Java applet中捕获和解码条形码。它工作正常一次,但每当我尝试在浏览器中重新加载/刷新我的Java小程序时,我想之前的事情都没有发布,因此我得到以下例外:
Exception in thread atomic-processor-1
Exception in thread webcam-updater-thread-1
Exception in thread webcam-panel-scheduled-executor-1
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at java.util.concurrent.LinkedBlockingQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signal(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-WebcamQRCodeExample.class-1" java.lang.NullPointerException
at java.awt.EventQueue.isDispatchThread(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDT(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.doClearAppletArea(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
以下是我的applet代码:
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class WebcamQRCodeExample extends JApplet implements Runnable, ThreadFactory {
private static final long serialVersionUID = 6441489157408381878L;
private Executor executor = Executors.newSingleThreadExecutor( this);
private volatile boolean running = true;
private Webcam webcam = null;
private WebcamPanel panel = null;
private JTextArea textarea = null;
private Thread t = null;
public WebcamQRCodeExample() {
super();
setLayout( new FlowLayout());
webcam = Webcam.getWebcams().get( 0);
panel = new WebcamPanel( webcam);
add( panel);
setVisible( true);
executor.execute( this);
}
@Override
public void run() {
do {
try {
Thread.sleep( 20);
} catch ( InterruptedException e) {
e.printStackTrace();
}
Result result = null;
BufferedImage image = null;
if ( webcam.isOpen()) {
if ( ( image = webcam.getImage()) == null) {
continue;
}
LuminanceSource source = new BufferedImageLuminanceSource( image);
BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer( source));
try {
result = new MultiFormatReader().decode( bitmap);
} catch ( NotFoundException e) {
// fall thru, it means there is no QR code in image
}
}
if ( result != null) {
textarea.setText( result.getText());
}
} while ( running);
}
@Override
public Thread newThread( Runnable r) {
t = new Thread( r, "example-runner");
t.setDaemon( true);
return t;
}
public static void main( String[] args) {
new WebcamQRCodeExample();
}
public void destroy() {
try {
running = false;
if ( webcam != null) {
while ( webcam.isOpen()) {
webcam.close();
webcam.resetDriver();
}
webcam = null;
}
} catch ( Exception e) {
e.printStackTrace();
}
}
public void stop() {
try {
running = false;
while ( webcam.isOpen()) {
webcam.close();
webcam.resetDriver();
}
if ( webcam != null)
webcam = null;
System.out.println( "mmmmmm");
} catch ( Exception e) {
e.printStackTrace();
}
}
}
当我重新加载applet时,如何避免上述异常?如何在重新加载applet之前等待几秒钟,以防止IllegalMonitorStateException
?谢谢。