スレッドと同期
public void move(int from, int to, long amount)
{
if (stocks[from] < amount) amount=stocks[from];
stocks[from] -= amount;
stocks[to] += amount;
}
void startButton_actionPerformed(ActionEvent e) {
int i;
label1.setText(" ");
stock.init();
for (i = 0; i < 10; i++)
{
t[i] = new MoveThread(stock, i,1000);
t[i].setPriority(Thread.NORM_PRIORITY + i % 2);
t[i].start();
}
checkbox1.setVisible(true);
}
スレッドの停止は次のように行います。stopThread();で、スレッドのstopFをセットし、各スレッドで実行を停止します。この処理を行うため各スレッドをt[]で記録しています。void button1_actionPerformed(ActionEvent e) {
for(int i=0;i<10;i++) {
t[i].synchThread(false);
t[i].stopThread();
}
checkbox1.setState(false);
checkbox1.setVisible(false);
}
public void move(int th,int from, int to, long amount)
{
if (stocks[from] < amount) amount=stocks[from];
stocks[from] -= amount;
lbl2.setText("Id:"+th+ " From:" + from+ " To: " + to + " amt:"+amount);
stocks[to] += amount;
nMove++;
test();
}
このとき、途中にsetTextで表示を行い、他のスレッドの割り込みを起こし安くしています。一方