CurrentInstance - Custom usage

Hello,

I want to use CurrentInstance to add a threadlocal class instance on my own (I want to be able to use it just like “UI.getCurrent()”).

This should happen inside an addon project.

I am not sure on how/when to add the instance to CurrentInstance.

Example:

public class MyTracker {
    public static MyTracker getCurrent() {

      // here?

      MyTracker instance = CurrentInstance.get(MyTracker.class);
      if(instance == null) {
          
          // Threadsafety on parallel calls of "getCurrent()" ?

          instance = new MyTracker();
          CurrentInstance.set(MyTracker.class, instance);
      }

      return instance;
    }
}