public abstract class DataThread<E> implements Runnable {
private Thread thread = null;
+ private final Object threadLock = new Object();
private BlockingQueue<E> dataQueue = new LinkedBlockingQueue<E>();
}
}
- public synchronized final boolean start() {
- if (isThreadAlive()) {
- try {
- thread.interrupt();
- thread.join();
- thread = null;
- } catch (InterruptedException e) {
- Logger.exception(e);
- return false;
+ public final boolean start() {
+ synchronized (threadLock) {
+ if (isThreadAlive()) {
+ try {
+ thread.interrupt();
+ thread.join();
+ thread = null;
+ } catch (InterruptedException e) {
+ Logger.exception(e);
+ return false;
+ }
}
- }
- clearQueue();
- resetBeforeStart();
+ clearQueue();
+ resetBeforeStart();
- thread = new Thread(null, this, getThreadName());
- thread.start();
+ thread = new Thread(null, this, getThreadName());
+ thread.start();
- return workAfterStart();
+ return workAfterStart();
+ }
}
- public synchronized final boolean stopNormal() {
- if (isThreadAlive()) {
- try {
- thread.join();
- thread = null;
- Logger.debug(getThreadName() + " thread joined!"); //$NON-NLS-1$
- } catch (InterruptedException e) {
- Logger.exception(e);
- return false;
+ public final boolean stopNormal() {
+ synchronized (threadLock) {
+ if (isThreadAlive()) {
+ try {
+ thread.join();
+ thread = null;
+ Logger.debug(getThreadName() + " thread joined!"); //$NON-NLS-1$
+ } catch (InterruptedException e) {
+ Logger.exception(e);
+ return false;
+ }
}
- }
- clearQueue();
- clearAfterStop();
+ clearQueue();
+ clearAfterStop();
- return workAfterStopNormal();
+ return workAfterStopNormal();
+ }
}
- public synchronized final boolean stopForced() {
- if (isThreadAlive()) {
- try {
- thread.interrupt();
- thread.join();
- thread = null;
- Logger.debug(getThreadName() + " thread joined!"); //$NON-NLS-1$
- } catch (InterruptedException e) {
- Logger.exception(e);
- return false;
+ public final boolean stopForced() {
+ synchronized (threadLock) {
+ if (isThreadAlive()) {
+ try {
+ thread.interrupt();
+ thread.join();
+ thread = null;
+ Logger.debug(getThreadName() + " thread joined!"); //$NON-NLS-1$
+ } catch (InterruptedException e) {
+ Logger.exception(e);
+ return false;
+ }
}
- }
- clearQueue();
- clearAfterStop();
+ clearQueue();
+ clearAfterStop();
- return workAfterStopForced();
+ return workAfterStopForced();
+ }
}
public boolean isThreadAlive() {