title: Limiting concurrent access to a field
body: <p>So in a program I'm writing I use a bi-directional breadth first search to search a graph. I do this by running 1 breadth first search in 1 thread, and one in another. Now the search is said to have found the optimal solution when it either an element from the other search is hit, or when the goal is found (which never really happens, but just in case it does for some reason..).</p>
  
  <p>The problem I am running into, is that I need to save this optimal solution to a field, because I need to continue to find all of the solutions, but the field value is getting messed up because both threads hit it at the same time (I think).</p>
  
  <p>Is there a way to block access to the thread who gets there last? I've tried using an AtomicReference and its compareAndSet method but that didn't do the trick. The value still gets messed up....</p>
  
  <p>Btw I'm using java, and for the threads I'm using Callable objects.</p>