I need to make an application that generates a list containing the following characters: H,A,O,*,#,@. The last two are control characters. The "#" makes the list divide in two (like a tree) and each sublist have to continue filling until the "@" appears, which indicates the list is finish. This process continues until all of the sublists have the "@" or the lenght of all the sublists together are 10000.
Then I have to iterate through the list using threads, and for each of the characters paint a different image in a frame. When I find a "#", the thread have to start two other threads to iterate throught the next two sublists and it have to terminate.
I am getting stuck in the part when I have to start the new threads and finish the first one. Googling I found a way to do this, but I feel that is a bad way to do it. So my question is: Is there another way to make this?
public class PainterThread implements Runnable {
public PainterThread(Iterator pIterator, ExecutorService pPool, PaintingPanel pPanel) {
_iteratorList = pIterator; //the Iterator of the list
_Pool = pPool; //I pass the ThreadPool as a parameter in order to make the request to create new threads
_PanelToPaint = pPanel;// The Panel which going to paint the images
}
//Methods
@Override
public void run() {
while (_iteratorList.hasNext()) {
Node actualNode = (Node) _iteratorList.next();
switch (actualNode.getControlSign()) {
case "H":
_PanelToPaint.addShapes(new Square(actualNode.getPositionX(), actualNode.getPositionY(), actualNode.getSize()));
_PanelToPaint.repaint();
break;
case "A":
_PanelToPaint.addShapes(new Triangle(actualNode.getPositionX(), actualNode.getPositionY(), actualNode.getSize()));
_PanelToPaint.repaint();
break;
case "O":
_PanelToPaint.addShapes(new Circle(actualNode.getPositionX(), actualNode.getPositionY(), actualNode.getSize()));
_PanelToPaint.repaint();
break;
case "*":
_PanelToPaint.addShapes(new Asterisk(actualNode.getPositionX(), actualNode.getPositionY(), actualNode.getSize()));
_PanelToPaint.repaint();
break;
case "#":
LinkedList actualList = (LinkedList) _iteratorList.next();
_Pool.submit(new PainterThread(actualList.iterator(), _Pool,_PanelToPaint)); //Here is my question. Is there another way to make this?
if (_iteratorList.hasNext()) {
actualList = (LinkedList) _iteratorList.next();
_Pool.submit(new PainterThread(actualList.iterator(), _Pool, _PanelToPaint));
}
break;
case "@":
break;
}
}
}
public void setIterator(Iterator pNewIterator) {
_iteratorList = pNewIterator;
}
//Atributes
private Iterator _iteratorList;
private ExecutorService _Pool;
private PaintingPanel _PanelToPaint;
}