Question: In the below example, I want the Element object to keep track of list of threads who have "marked" the elements. The field variable `name` cannot be changed unless no thread is in the `markedThreadList`. Thread can mark or unmark itself on this element, which adds or removes the thread from the list.

```
class Element {
    private String name;
    private LinkedList<?> markedThreadList; //list of threads who have marked
    public mark(){
        //add the thread to the markedThreadList
    }

    public unmark(){
        //caller thread removes itself from the markedThreadList
    }

    public void setName(String name){ 
        if(markedThreadList.size()==0){
            this.name = name;
        }
    }
}

```

My question is, how can I store the thread lists? What type should I use to store them? Should I just give unique integers to each thread when it starts running, and use that information?

Answer: 
Because multiple threads are modifying a single `Element` instance, you need to consider concurrency issues carefully. Since you have two variables that interact (the element name can only be set if there are no marks), you'll need to synchronize on some lock. Otherwise, you could use a concurrent collection like `ConcurrentHashMap` to store the set of marks.

```
final class Element {

  private final Set<Thread> marks = new HashSet<Thread>();

  private String name;

  public boolean mark() {
    synchronized(marks) {
      return marks.add(Thread.currentThread());
    }
  }

  public boolean unmark(){
    synchronized(marks) {
      return marks.remove(Thread.currentThread());
    }
  }

  public boolean setName(String name){ 
    synchronized(marks) {
      if (marks.isEmpty()) {
        this.name = name;
        return true;
      } else
        return false;
    }
  }

}

```
Here's an example:

```
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Collections;

class Element implements Iterable<Thread> {
    private String name;
    private final Set<Thread> markedThreads = Collections.synchronizedSet(new HashSet<Thread>());

    public void mark(){
      markedThreads.add(Thread.currentThread());
    }

    public void unmark(){
      markedThreads.remove(Thread.currentThread());
    }

    public synchronized void setName(String name){ 
        if(markedThreads.isEmpty()){
            this.name = name;
        }
    }

    public Iterator<Thread> iterator() {
      return markedThreads.iterator();
    }
}

```

The point I'd think you'd want to note are that because it implements `hashCode` and `equals` correctly, instances of the `Thread` class can be used directly in typical Java collections, such as `HashSet`, without needing to give them identifiers like integers. And the static method `Thread.currentThread` returns the current `Thread` object.