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: 
A thread is an instance of `java.lang.Thread`. Use a `Set<Thread>` (rather than a List). You can access the current thread using `Thread.currentThread()`.
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.