Java暂停执行

Java暂停执行,java,multithreading,Java,Multithreading,我有一个独立工作的线程池。基本上,这些线程都是从网站获取数据。我还有另一个线程更改我的系统IP。我只需要在另一个线程更改ip时暂停所有其他线程。一旦ip发生变化,其他线程池就会恢复 有什么解决办法吗 这是我的密码: for(;;){ for (int aa = 0; aa < pages.size(); aa++) { if (pageID != null) { t = new Thread(new BackgroundThread(pageID.get(aa)))

我有一个独立工作的线程池。基本上,这些线程都是从网站获取数据。我还有另一个线程更改我的系统IP。我只需要在另一个线程更改ip时暂停所有其他线程。一旦ip发生变化,其他线程池就会恢复

有什么解决办法吗

这是我的密码:

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++;
    }
  }
}
(;;)的
{
对于(int aa=0;aa
使用某种读/写锁怎么样?线程以读卡器的身份完成正常工作(在相对较小的块中,因此它们可以及时中断),而需要更改IP的线程则以写卡器的身份完成正常工作。

在java 1.5 api中尝试此类。

我假设你真的是在更改机器IP?您需要确保线程处于可以更改系统IP的状态,并且系统IP转换器需要等待所有线程暂停以更改IP

这可以通过使用倒计时闩锁来指示线程应该暂停-每个线程倒计时闩锁。当所有线程都达到锁存状态时,系统IP更新程序可以继续。一旦完成工作,它就会通过将闩锁设置为null来恢复线程

为了清楚起见,省略了异常处理

  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();
    }
  }
类SystemIPChanger实现可运行{
收集池;
公共无效更改IP(){
pauseallhtreads();
changeIP();
恢复线程();
}
void pauseallhtreads(){
CountDownLatch latch=新的CountDownLatch(pool.size());
for(WorkerThread worker:pool){
worker.setPause(闩锁);
}
satch.wait();
}
void resumeThreads(){
for(WorkerThread worker:pool){
worker.setPause(空);
}
}
}
类WorkerThread实现Runnable{
私人倒计时暂停;
公开募捐{
而(…){
暂停();
doRealWork();
}
}
同步无效暂停(){
倒计时闩锁=暂停;
如果(闩锁!=null){
倒计时();
while(暂停==锁存){
等待();
}
}
}
公共同步无效设置暂停(倒计时闩锁){
this.pause=闩锁;
notifyAll();
}
}

我认为您在这里做的事情是非法的:)无论如何-线程中有
而(true)
吗?展示一些代码。积极地思考,积极地思考!!我正在更改我的NIC IP不是非法的东西!!!;-)请使用代码标记。@Bozho,除非问题是“我如何违法?”我想我们不能假设任何人做了任何违法的事情。谢谢你的回答。我已经编辑了我的问题。请查看并建议我如何实现读/写锁@rmarimon建议,Java自己的ReadWriteLock(如ReentrantReadWriteLock:)的实现是显而易见的答案,可能是最好的选择。不过,如果您发现还需要其他功能,可能还有其他的库提供更专业的版本。