也许我犯了一个错误。但我无法解决这个问题。 Swing Worker 类中的 for
循环不起作用。
public class _5Strategy_A extends SwingWorker<Integer, Integer> {
_4HttpRequest_Naver attk = new _4HttpRequest_Naver();
int num_acc;
int time ;
int total_post;
int total_comment;
int init = 0;
int execute = 0;
int i = 0;
int c = 0;
static int k = 0;
static int response = 0;
private boolean _suspended = false;
public synchronized void suspend() { _suspended = true; notify(); }
public synchronized void resume() { _suspended = false; notify(); }
@Override
protected Integer doInBackground() throws Exception {
init = 0;
publish(new Integer[] { init }); //
_1SetProxy.setProxy();
init = 1;
publish(new Integer[] { init }); //
_3SetTarget_NaverPopular25.setTarget_Naver();
_3SetTarget_NaverRise50.setTarget_NaverUp50();
new _3SetTarget_NaverRise50(10);
// ***************************************************************** //
// ***************************************************************** //
for (int ch=0; ch<5; ch++){
System.out.println("Obviously This statement could be shown 5 times.")
// ***************************************************************** //
for (k=0; k<2; k++){
synchronized(this) {
while (_suspended == true) {
wait(); // The current thread will block until some else calls notify()
// Then if _suspended is false, it keeps looping the for
}
}
init = 2;
publish(new Integer[] { init });
String page = attk.GetLoginPage_Naver(_0Storing.url);
String raw = String.valueOf(_9AccountsManager.tableNaverAccounts_A.getModel().getValueAt(0, num_acc));
String[] raw_splited = raw.split(":");
String id = raw_splited[0];
String pw = raw_splited[1];
String postParams = attk.getFormParams(page, id, pw);
CookieHandler.setDefault(new CookieManager());
init = 3;
publish(new Integer[] { init });
// POST
attk.loginNaver(_0Storing.url, postParams);
response = 0; // Initializing response code.
init = 4;
publish(new Integer[] { init });
// POST
try {
attk.AttackNaver(_0Storing.baseURL, String.valueOf(GUI.proxyTable.getModel().getValueAt(k%17, 0)), Integer.parseInt(String.valueOf(GUI.proxyTable.getModel().getValueAt(k%17, 1))), _3SetTarget_NaverPopular25.attkParam[k%34]);
} catch (Exception e) {
}
System.out.println(+k+" looping.");
}
num_acc++;
k=0;
init=1;
}
// ***************************************************************** //
// ***************************************************************** //
return null;
} // End of doInBackground()
protected void process(List<Integer> chunks) {
/* Initialization */
if(init==0){
_0Storing.addRow("GET", "Checking", _1SetProxy.proxy_url, "LOCAL");
}
if(init==1){
_0Storing.setRst("OK");
_0Storing.addRow("GET", "Targetting", "http://finance.naver.com/sise/lastsearch2.nhn", "LOCAL");
}
if(init==2){
_0Storing.setRst("OK");
_0Storing.addRow("GET", "Extracting", _0Storing.url, "LOCAL");
}
if(init==3){
_0Storing.setRst("OK");
_0Storing.addRow("POST", "Login...("+_9AccountsManager.tableNaverAccounts_A.getModel().getValueAt(0, num_acc)+")", _0Storing.url, "LOCAL");
}
if(init==4){
_0Storing.setRst("OK");
try {
_0Storing.addRow("POST", _3SetTarget_NaverPopular25.text[k%24], _3SetTarget_NaverPopular25.code[k%24], String.valueOf(GUI.proxyTable.getModel().getValueAt(k%17, 0)));
} catch (ArrayIndexOutOfBoundsException e){
}
}
}
@Override
protected void done() {
if (isCancelled())
_0Storing.addLog("\nCancelled !");
else{
GUI.bottomStatus.setText("Finish !");
if (response == 0)
GUI.mainTable.setValueAt("OK", GUI.mainTable.getRowCount()-1, 5);
else
GUI.mainTable.setValueAt("FAILED", GUI.mainTable.getRowCount()-1, 5);
}
}
}
上面的代码是我的代码。抱歉又脏又复杂。但我无法总结它,因为我不知道这个问题的原因是什么。请专注于//*//for 循环。也许我的语句可以在控制台中显示 5 次。但我只能看到 1 次。请告诉我为什么这个 for 循环不起作用。谢谢。
最佳答案
以下两个 block 在同一对象 (this
) 上同步
:
public synchronized void resume() { _suspended = false; notify(); }
和
synchronized(this) {
while (_suspended == true) {
wait(); // The current thread will block until some else calls notify()
// Then if _suspended is false, it keeps looping the for
}
}
所以你陷入了僵局。当 SwingWorker 处于 wait()
状态时,您将永远无法 resume()
,因为同步不允许这样做。有关同步的更多信息,我建议查看Oracle tutorial特别是关于 Intrinsic Locks 的部分
顺便说一句,我不会再用这段代码做任何恶事。看到 attk = new _4HttpRequest_Naver();
关于java - 在 Swing Worker 类中不适用于循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22011955/