ton1
ton1

Reputation: 7628

Doens't working for loop in swing worker class

Maybe there is a mistake I did. But I can't fix this problem. The for loop in Swing Worker Class doesn't working.

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);
    }
   }

}

Above code is my code. Sorry to dirty and complicated. but I can't summary it because I don't know what is the cause of this problem. please concentrate on //*// for loop. maybe my statement could be shown in console 5 times. but I can see that only 1 time. Please Tell me why this for loop doesn't working. Thank you.

Upvotes: 0

Views: 115

Answers (2)

Nick Rippe
Nick Rippe

Reputation: 6465

Both of the following blocks are synchronized on the same object (this):

public synchronized void resume() { _suspended = false; notify(); }

and

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
    }
}

So you have a deadlock. You'll never be able to resume() while the SwingWorker is in the wait() state because synchronization won't allow it. For more information on synchronization, I'd recommend looking at the Oracle tutorial and specifically the section on Intrinsic Locks

On a side note, I wash my hands clean of any evil you're planning to do with this code. It did make me chortle to see attk = new _4HttpRequest_Naver();

Upvotes: 1

Jorge_B
Jorge_B

Reputation: 9872

It is a matter of taste, but I don't actually like the way concurrency is managed here. I suggest you eliminate all that loops, the wait(), etc., and just make your SwingWorker to block itself into a BlockingQueue reading. Something like:

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

This way you manage your concurrency using high-level java facilities and can focus in doing whatever is your heavy task. And this way, it escalates easily if you need more Workers in some moment.

Upvotes: 0

Related Questions