title: Only one thread have access to a certain part of code?
body: <p>I have a project which requires logging in my router from time to time and make some changes. In eclipse I have a separate project which deals with all the things I need changed and I have that in the build path of my main project which is a multi threaded project. My problem is that sometimes two threads try to access the router which messes things up. Is there anything I can do that only one thread can access a certan part of my code.</p>
  
  <p>This is the related code for the main multithreaded application</p>
  
  <pre><code>if (totalLogins &gt; 10)
  {
    IpManager.rebootRouter();
    Thread.sleep(4000);
    totalLogins = 0;
    composedMessagesWithThisIP = 0;
  }
  </code></pre>
  
  <p>And in the other project this is what I have</p>
  
  <pre><code>public synchronized static void rebootRouter()
  {
    try
    {
      //code related to restart modem
    }
    catch (Exception e)
    {
  
    }
  }
  </code></pre>
  
  <p>So what I have done is made the method synchronized but I believe from time to time more than one thread access the "rebootRouter" method. Which causes problems in my main application.</p>
  
  <p>What is the best way and most effective way to make IpManager.rebootRouter() be executed by one thread only?</p>
  
  <p>Regards!</p>