title: Is synchronized enough to make the drainTo() method of a BlockingQueue atomic?
body: <p>If I simply do something like this:</p>
  
  <pre><code>synchronized(taskQueue) { //taskQueue is a BlockingQueue 
    taskQueue.drainTo(tasks); //tasks is a list
  }
  </code></pre>
  
  <p>Am I assured that concurrent calls to <code>taskQueue.put()</code> and <code>taskQueue.take()</code> can not be executed inside the synchronized block?</p>
  
  <p>In other words, am I making the drainTo() method atomic?</p>
  
  <p><strong>Or more generally, how do I make a composition of thread safe operations atomic?</strong></p>
  
  <p>Example:</p>
  
  <pre><code>if(taskQueue.size() == 1) {
     /*Do a lot of things here, but I do not want other threads
       to change the size of the queue here with take or put*/
  }
  //taskQueue.size() must still be equal to 1
  </code></pre>