Java 多恩斯';在swing worker类中为循环工作
也许我犯了个错误。但我不能解决这个问题。Swing Worker类中的Java 多恩斯';在swing worker类中为循环工作,java,swing,http,swingworker,Java,Swing,Http,Swingworker,也许我犯了个错误。但我不能解决这个问题。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
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);
}
}
}
public class\u 5Strategy\u A扩展SwingWorker{
_4htprequest_Naver attk=new _4htprequest_Naver();
国际会计准则;
整数时间;
国际总员额;
国际总评论;
int init=0;
int执行=0;
int i=0;
int c=0;
静态int k=0;
静态int响应=0;
私有布尔值_suspended=false;
公共同步的void suspend(){u suspended=true;notify();}
公共同步的void resume(){u suspended=false;notify();}
@凌驾
受保护的整数doInBackground()引发异常{
init=0;
发布(新整数[]{init})//
_1SetProxy.setProxy();
init=1;
发布(新整数[]{init});//
_3SetTarget_NaverPopular25.setTarget_Naver();
_3SetTarget_NaverRise50.setTarget_NaverUp50();
新的设定目标50(10);
// ***************************************************************** //
// ***************************************************************** //
对于(int ch=0;ch这是一个品味问题,但我实际上不喜欢这里管理并发的方式。我建议您消除所有循环、wait()等,让您的SwingWorker将自己阻塞到阻塞队列读取中。类似于:
public static BlockingQueue<WorkerCommand> queue =
new ArrayBlockingQueue<WorkerCommand>();
// WorkerCommand is a simple bean with a getter and a setter for the command
// ....
boolean keepOn = true;
while (keepOn) {
WorkerCommand command = queue.poll(); // blocking!
if (command.getCommand().equals(COMMAND_STOP)) { keepOn = false; }
else {
// whatever you need
}
}
// Clients just put commands into the queue, which makes the worker to wake up, consume and
// serve the command
公共静态阻止队列=
新建ArrayBlockingQueue();
//WorkerCommand是一个简单bean,具有getter和命令的setter
// ....
布尔值keepOn=true;
while(keepOn){
WorkerCommand=queue.poll();//阻塞!
如果(command.getCommand().equals(command_STOP)){keepOn=false;}
否则{
//你需要什么都行
}
}
//客户机只是将命令放入队列中,这使工作人员能够唤醒、消费和恢复
//服从命令
通过这种方式,您可以使用高级java工具来管理并发性,并可以专注于完成繁重的任务。通过这种方式,如果您在某个时刻需要更多的工作人员,它将很容易升级。以下两个块都是在同一个对象上同步的(This
):
及
因此您出现了死锁。当SwingWorker处于wait()
状态时,您将永远无法resume()
,因为同步不允许。有关同步的详细信息,我建议您查看,特别是
顺便说一句,我洗手不干你打算用这段代码做的任何坏事。当我看到attk=new(http://prequest\u Naver();
开始时,你漏掉了那句话后面的“;”。这不是你的代码。如果是你的代码,你会知道锁的原因。我觉得这段代码很可疑。
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
}
}