Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these correctly declares a variable holding a per-thread value?

  • A.static String user = new ThreadLocal();
  • B.ThreadLocal<String> user = new ThreadLocal<String>();
  • C.ThreadLocal user = Thread.local();
  • D.ThreadLocal<String> user = ThreadLocal.get();

Answer: B

ThreadLocal is a generic class instantiated with new, and its type argument is what get and set exchange. There is no factory on Thread, and get is an instance method returning the stored value rather than a constructor.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.