1

Is it possible to pause all other threads running while a thread is doing something specific? For making my question lets assume I have the following method

public void doActions()
{
  ......
  ......
  ......
  if(count == 30)
  {
    //This is where I want to pause all other running threads

  }
  ......
  ......
  ......
}

Regards!

Arya
  • 8,473
  • 27
  • 105
  • 175
  • Are all threads the same? You could use a semaphore structure to allow one thread to enter a block and then flip the semaphore off so as to block other threads from entering the same code block. Or similarly use a synchronized block. http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html – Girish Rao Jun 10 '12 at 07:25
  • 6
    Don't ask for a solution, rather describe your objective. You're most likely trying to solve something the wrong way. – Lucero Jun 10 '12 at 07:27
  • 1
    Why do you need to pause other threads - the main question. Based on this we could give you our thoughts. – Eugen Martynov Jun 10 '12 at 07:28
  • The generic solution of just pausing other threads independent of what they do is no longer supported by the java api as that is prone to deadlocks. You have to be more specific to get a usable solution for your problem. – josefx Jun 10 '12 at 10:18
  • This is a network application which I'm working on and in a certain block of the program it reboots the router which causes the network to go down. This is why I wanted to pause all other threads until the router is fully rebooted. – Arya Jun 11 '12 at 15:21

2 Answers2

0
  1. You can try using CountDownLatch, which will helps you in scenarios where you want to access few resources before the other processes kicks off.

  2. Well you can also try to use Semaphore to give access to only one thread at a time using

    Semaphore s = new Semaphore(1);

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

I posted a PauseableThread as a possible solution to another question recently. Although it works slightly differently to what you want (you need one thread that stops a number of others while it has a number of other threads that can stop one thread), turning it into what you are looking for should be quite simple.

Note that the other threads will only pause at predefined points, they won't just stop where they are.

Community
  • 1
  • 1
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213