com.sun.electric.tool.util.concurrent.patterns.PWhileJob Maven / Gradle / Ivy
/* -*- tab-width: 4 -*-
*
* Electric(tm) VLSI Design System
*
* File: PWhileJob.java
*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*
* Electric(tm) is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Electric(tm) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Electric(tm); see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, Mass 02111-1307, USA.
*/
package com.sun.electric.tool.util.concurrent.patterns;
import com.sun.electric.tool.util.concurrent.datastructures.IStructure;
/**
*
* While job, do the work parallel while there are elements in the queue
*
* @param
*
* @author Felix Schmidt
*/
public class PWhileJob extends PJob {
private IStructure items;
private PWhileTask task;
public PWhileJob(IStructure items, PWhileTask task) {
this.items = items;
this.task = task;
}
/*
* (non-Javadoc)
*
* @see com.sun.electric.tool.util.concurrent.patterns.PJob#execute()
*/
@Override
public void execute() {
int numOfThreads = this.pool.getPoolSize();
for (int i = 0; i < numOfThreads; i++) {
this.add(new WhileTaskWrapper(this, task, items), i);
}
super.execute();
}
public abstract static class PWhileTask extends PTask implements Cloneable {
/**
* @param job
*/
public PWhileTask(PJob job) {
super(job);
}
public PWhileTask() {
super(null);
}
public abstract void execute(T item);
@Override
public final void execute() {
}
}
/**
*
* Wrapper class for {@link PWhileTask} (internal)
*
* @param
*/
public final static class WhileTaskWrapper extends PTask {
private IStructure items;
private PWhileTask task;
/**
* @param job
*/
public WhileTaskWrapper(PJob job, PWhileTask task, IStructure items) {
super(job);
this.items = items;
this.task = task;
}
/*
* (non-Javadoc)
*
* @see com.sun.electric.tool.util.concurrent.patterns.PTask#execute()
*/
@SuppressWarnings("unchecked")
@Override
public void execute() {
T item = null;
item = items.remove();
while (item != null) {
try {
PWhileTask tmp = (PWhileTask) task.clone();
tmp.execute(item);
item = items.remove();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
return;
}
}
}