T
threadExec
Gast
Hallo,
ich habe mich heute mit einem kleinen Programm rumgeschlagen,
um zu testen wie ich am besten einen Threadpool verwende und
gleichzeitig die im Threadpool enthaltenen Thrads pausieren und
stoppen kann.
Ich habe nun allerdings das Problem, dass sich das Programm
nach dem Fortsetzen (d. h. Fortsetzen nachdem ich es pausiert habe)
nicht mehr bedienbar ist.
Auch zuvor läuft es nicht wirklich flüssig.
Ein kleines kompilierbares Beispiel:
Kann mir jemand sagen wo da das Problem ist?
Nebenbei noch eine Frage, ich dachte, dass der Threadpool immer nur
so viele Runnables annimmt, wie der Threadpool groß ist (in meinem Falle 10),
wenn man nun aber Pause klickt sieht man am output in der Konsole, das
alle 1000 bereits pausiert werden (ich gehe da ja immer bis zum index
und wenn der Threadpool wirklich nur immer 10 übernehmen würde wäre
der index zumindest beim ersten mal noch 10)
Wäre cool wenn mir auch das jemand erläutern könnte
ich habe mich heute mit einem kleinen Programm rumgeschlagen,
um zu testen wie ich am besten einen Threadpool verwende und
gleichzeitig die im Threadpool enthaltenen Thrads pausieren und
stoppen kann.
Ich habe nun allerdings das Problem, dass sich das Programm
nach dem Fortsetzen (d. h. Fortsetzen nachdem ich es pausiert habe)
nicht mehr bedienbar ist.
Auch zuvor läuft es nicht wirklich flüssig.
Ein kleines kompilierbares Beispiel:
Java:
public class MyRunnable implements Runnable {
private boolean isWaiting = false;
private boolean isStopped = false;
private String name = "";
public MyRunnable(String name) {
this.name = name;
}
public void run() {
int i = 0;
while(!isStopped) {
isWaiting();
System.out.println("Running: " + name + " , Counter: " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Interrupted: " + name);
}
i++;
}
}
/**
*
*/
public synchronized void pauseProcess() {
isWaiting = true;
System.out.println("Paused: " + name);
}
/**
*
*/
public synchronized void continueProcess() {
isWaiting = false;
notify();
}
/**
*
*/
private synchronized void isWaiting() {
if(isWaiting) {
try {
wait();
} catch (InterruptedException e) {}
}
}
public synchronized boolean getIsWaiting() {
return isWaiting;
}
public synchronized void setIsWaiting(boolean isWaiting) {
this.isWaiting = isWaiting;
}
public synchronized boolean getIsStopped() {
return isStopped;
}
public synchronized void setIsStopped(boolean isStopped) {
this.isStopped = isStopped;
}
}
Java:
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Controller extends Thread {
private Queue<String> queue = null;
private ExecutorService exec = null;
private List<MyRunnable> runnables = null;
private boolean isStopped = false;
private int index = 0;
public Controller(Queue<String> queue) {
this.queue = queue;
this.exec = Executors.newFixedThreadPool(10);
this.runnables = new LinkedList<MyRunnable>();
while(!queue.isEmpty()) {
runnables.add(new MyRunnable(queue.poll()));
}
}
public void run() {
while(!runnables.isEmpty() && !isStopped) {
exec.execute(runnables.get((index < 998 ? index++ : index)));
}
}
public void pauseProcess() {
System.out.println("Controller triggered Pause!");
for(int i = 0; i < index; i++) {
System.out.println("Controller Pause: " + i);
runnables.get(i).setIsWaiting(true);
}
}
public void resumeProcess() {
for(int i = 0; i < index; i++) {
System.out.println("Controller Resume: " + i);
runnables.get(i).setIsWaiting(false);
}
}
public void stopProcess() {
isStopped = true;
for(int i = 0; i < index; i++) {
runnables.get(i).setIsStopped(true);
}
}
}
Java:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class View extends JFrame implements ActionListener {
private Controller controller = null;
private JButton buttonStart = new JButton("Start");
private JButton buttonPause = new JButton("Pause");
private JButton buttonResume = new JButton("Resume");
private JButton buttonStop = new JButton("Stop");
public View(Controller controller) {
this.controller = controller;
buttonStart.addActionListener(this);
buttonPause.addActionListener(this);
buttonResume.addActionListener(this);
buttonStop.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(400, 400));
this.setBounds(100, 100, 400, 400);
this.setLayout(new GridLayout(1, 0));
this.setVisible(true);
this.add(buttonStart);
this.add(buttonPause);
this.add(buttonResume);
this.add(buttonStop);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source.equals(buttonStart)) {
Thread thread = new Thread(controller);
thread.start();
} else if(source.equals(buttonPause)) {
controller.pauseProcess();
} else if(source.equals(buttonResume)) {
controller.resumeProcess();
} else if(source.equals(buttonStop)) {
controller.stopProcess();
}
}
}
Java:
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
for(int i = 0; i < 1000; i++) {
queue.offer("" + i);
}
new View(new Controller(queue));
}
}
Kann mir jemand sagen wo da das Problem ist?
Nebenbei noch eine Frage, ich dachte, dass der Threadpool immer nur
so viele Runnables annimmt, wie der Threadpool groß ist (in meinem Falle 10),
wenn man nun aber Pause klickt sieht man am output in der Konsole, das
alle 1000 bereits pausiert werden (ich gehe da ja immer bis zum index
und wenn der Threadpool wirklich nur immer 10 übernehmen würde wäre
der index zumindest beim ersten mal noch 10)
Wäre cool wenn mir auch das jemand erläutern könnte