001
014
015 package com.liferay.portal.kernel.concurrent.test;
016
017 import java.util.concurrent.CountDownLatch;
018
019
022 public class MarkerBlockingJob implements Runnable {
023
024 public MarkerBlockingJob() {
025 this(false);
026 }
027
028 public MarkerBlockingJob(boolean blocking) {
029 this(blocking, false);
030 }
031
032 public MarkerBlockingJob(boolean blocking, boolean throwException) {
033 _blocking = blocking;
034 _throwException = throwException;
035 }
036
037 public Thread getRunThread() {
038 return _runThread;
039 }
040
041 public boolean isEnded() {
042 return _ended;
043 }
044
045 public boolean isInterrupted() {
046 return _interrupted;
047 }
048
049 public boolean isStarted() {
050 return _started;
051 }
052
053 @Override
054 public void run() {
055 _runThread = Thread.currentThread();
056
057 if (_started) {
058 throw new IllegalStateException("Job already started");
059 }
060
061 _started = true;
062
063 if (_blocking) {
064 _waitBlockingLatch.countDown();
065
066 try {
067 _blockingLatch.await();
068 }
069 catch (InterruptedException ie) {
070 _interrupted = true;
071 }
072 }
073
074 if (_throwException) {
075 throw new RuntimeException();
076 }
077
078 _ended = true;
079
080 _endedLatch.countDown();
081 }
082
083 public void unBlock() {
084 _blockingLatch.countDown();
085 }
086
087 public void waitUntilBlock() throws InterruptedException {
088 if (!_blocking) {
089 throw new IllegalStateException("Blocking is not enabled");
090 }
091
092 _waitBlockingLatch.await();
093 }
094
095 public void waitUntilEnded() throws InterruptedException {
096 _endedLatch.await();
097 }
098
099 private final boolean _blocking;
100 private final CountDownLatch _blockingLatch = new CountDownLatch(1);
101 private volatile boolean _ended;
102 private final CountDownLatch _endedLatch = new CountDownLatch(1);
103 private volatile boolean _interrupted;
104 private volatile Thread _runThread;
105 private volatile boolean _started;
106 private final boolean _throwException;
107 private final CountDownLatch _waitBlockingLatch = new CountDownLatch(1);
108
109 }