2

I have a pool of threads which are working independently. Basically these threads are getting data from websites. I have also another thread which change my System IP. I just need to pause all other threads while another thread is changing ip. As soon as ip will change then other threads pool will resume.

Is there any solution?

Here is my Code:

for(;;){
  for (int aa = 0; aa < pages.size(); aa++) {
    if (pageID != null) {
      t = new Thread(new BackgroundThread(pageID.get(aa)));
      System.out.println(pageID.get(aa));
      t.start();
    }
    if(ipNo == 3){
      ipNo = 0;
    }
    if(aa == 35) {
      //Following method take a little bit time to change ip. I need when this method will be executin then 
      //all above threads "t" will be paused
      IPRotator.rotateIP(ip[ipNo]); 
      //When IP will be change then all paused threads will be resumed
      ipNo++;
    }
  }
}
4
  • 3
    I think you are doing something illegal here :) Anyway - do threads have while(true) in them? Show some code. Commented Jul 3, 2010 at 21:13
  • Think positive Be Positve!! I am changing my NIC IP not illegal stuff!!! ;-) Commented Jul 3, 2010 at 21:25
  • Please use the markup for code. Commented Jul 3, 2010 at 21:48
  • @Bozho, unless the question is along the lines of "How to I break the law?" I don't think we can assume anyone is doing anything illegal. Commented Jul 3, 2010 at 22:01

3 Answers 3

2

I'm assuming you really mean you are changing the machine IP? You need to be sure that the threads are in a state where the system IP can be changed, and also that the system IP changer needs to wait until all threads are pausing for the IP to be changed.

This can be done using a CountDownLatch to indicate that threads should pause - each thread counts down the latch. When all threads have hit the latch, the system IP updater can proceed. Once it's done it's work, it then resumes the threads by setting the latch to null.

Exception handling omitted for clarity.

  class SystemIPChanger implements Runnable {
      Collection<WorkerThread> pool;

      public void changeIP() {
          pauseAllThreads();
          changeIP();
          resumeThreads();
      }

      void pauseAllThreads() {
          CountDownLatch latch = new CountDownLatch(pool.size());
          for (WorkerThread worker : pool) {
              worker.setPause(latch);
          }
          latch.await();          
      }

      void resumeThreads() {
          for (WorkerThread worker : pool) {
              worker.setPause(null);
          }

      }
  }

  class WorkerThread implements Runnable {
    private CountDownLatch pause;

    public void run() {
        while (...) {
            pause();
            doRealWork(); 
        }
    }


    synchronized void pause() {
        CountdownLatch latch = pause;
        if (latch!=null) {
           latch.countDown();
           while (pause==latch) {
               wait();
           }
        }
    }

    public synchronized void setPause(CountDownLatch latch) {
        this.pause = latch;
        notifyAll();
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

2

How about using some sort of Read/Write lock? The threads do their normal work (in relatively small chunks, so they can be interrupted in a timely fashion) as a reader, and the thread that needs to change IP does so as a writer.

2 Comments

Thankyou for your answer. I've edited my question. Please see it and advice me how can I implement read/write lock
As @rmarimon suggests, an implementation of Java's own ReadWriteLock (such as ReentrantReadWriteLock: java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/…) is the obvious answer and probably your best option. There are probably other libraries around with more specialist versions, though, should you find you need something else.
0

Try this class ReadWriteLock in the java 1.5 api.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.