Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class ParallelCappedRunnable implements Runnable {
- private final Lock lock;
- private final Condition condition;
- private final ParallelCappedMultithreader pcm;
- public ParallelCappedRunnable(ParallelCappedMultithreader pcm, Lock lock, Condition condition) {
- this.lock = lock;
- this.condition = condition;
- this.pcm = pcm;
- }
- @Override
- public final void run() {
- doTask();
- pcm.sendSignal();
- if(pcm.getMissingSignals() == 0) {
- lock.lock();
- try {
- condition.signal();
- } finally {
- lock.unlock();
- }
- }
- }
- public abstract void doTask();
- }
- public class ParallelCappedMultithreader {
- private AtomicInteger missingSignals = new AtomicInteger (0);
- private final int MAX_THREADS;
- private final Lock lock = new ReentrantLock();
- private final Condition condition = lock.newCondition();
- private final Class runnable, parentClass;
- private final Object parentInstance;
- public ParallelCappedMultithreader(Class<? extends ParallelCappedRunnable> runnable, int threads, Class parentClass, Object parentInstance) {
- this.runnable = runnable;
- this.parentClass = parentClass;
- this.MAX_THREADS = threads;
- this.parentInstance = parentInstance;
- }
- public ParallelCappedMultithreader(Class<? extends ParallelCappedRunnable> runnable, int threads) {
- this(runnable, threads, null, null);
- }
- public void start() throws InterruptedException{
- try {
- missingSignals.set(MAX_THREADS);
- Constructor constructor;
- if(parentClass == null) {
- constructor = runnable.getDeclaredConstructor(ParallelCappedMultithreader.class, Lock.class, Condition.class);
- }
- else {
- constructor = runnable.getDeclaredConstructor(parentClass, ParallelCappedMultithreader.class, Lock.class, Condition.class);
- }
- for(int i = 0; i < MAX_THREADS; i++) {
- if(parentClass == null)
- (new Thread((Runnable)constructor.newInstance(this, lock, condition))).start();
- else
- (new Thread((Runnable)constructor.newInstance(parentInstance, this, lock, condition))).start();
- }
- } catch (Exception ex) {
- Logger.getLogger(ParallelCappedMultithreader.class.getName()).log(Level.SEVERE, null, ex);
- System.exit(0);
- }
- lock.lock();
- try {
- condition.await();
- } finally {
- lock.unlock();
- }
- }
- public int getMissingSignals() {
- return missingSignals.get();
- }
- public void sendSignal() {
- System.out.println(missingSignals.getAndDecrement());
- }
- }
- MyRunnable(List threads) {
- lock (threads) {
- threads.add(this);
- }
- }
- void run() {
- doStuff()
- lock (threads) {
- threads.remove(this);
- if (threads.size() == 0)
- threads.notify();
- }
- }
- void startAndWait(int numThreads) {
- List<MyRunnable> threads = new ArrayList<MyRunnable>();
- for (int i=0; i<numThreads; i++)
- new Thread(new MyRunnable(threads)).start();
- lock (threads) {
- while (threads.size() > 0)
- wait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement