Home
Q&A
Education
Technology
Credit
General
Health
Insurance
Questions
Ask a Question
Why ThreadLocal doesn't create in new thread?
Home
Education
Why ThreadLocal doesn't create in new thread?
asked
Jun 2, 2022
in
Education
by
JackTerrance
The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves access only by the current thread. The class Collector is presented below: public class Collector { ThreadLocal
> storage; public Map collection(int id) { storage = new ThreadLocal<>(); storage.set(new HashMap<>()); for (int i = id * 100; i <= id * 1000; i+=id * 100) { storage.get().put(String.valueOf(i), "test"); } return storage.get(); } } I try to execute method collection(int id) at the same time in different threads. My suggestion is based the official documentation. But sometimes thrown NullPointerException, my observations point out on re-creating ThreadLocal by another Thread, so NullPointerException throw in the storage.get().put(String.valueOf(i), "test");, because in the another thread in the line storage = new ThreadLocal<>(); was re-initialized. Below presented code, where run two threads: Collector collector = new Collector(); new Thread(new Runnable() { @Override public void run() { System.out.println(collector.collection(1)); } }).start(); new Thread(new Runnable() { @Override public void run() { System.out.println(collector.collection(2)); } }).start(); How I can use the ThreadLocal to have local resource in each thread, which is independence from another threads? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)
javamultithreading
1
Answer
0
votes
answered
Jun 2, 2022
by
JackTerrance
The problem is that you're re-creating a new ThreadLocal instance each time the collection(int) method is called, with this.storage = new ThreadLocal<>(); The ThreadLocal
> can be a class static field private final static ThreadLocal
> STORAGE = new ThreadLocal<>(); To set and get the Thread's associated value, just use // You're better off passing and using an already constructed instance STORAGE.set(new HashMap<>()); STORAGE.get();
Related questions
0
votes
Q: Why ThreadLocal doesn't create in new thread?
The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked
May 27, 2022
in
Education
by
JackTerrance
javamultithreading
0
votes
Q: Pass argument when initializing ThreadLocal variable
In my Java application I want to have a pool of threads (I use ExecutorService for this) that connect ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
May 7, 2022
in
Education
by
JackTerrance
javamultithreading
0
votes
Q: When the executorService.shutdown(); should be called
We have a service method GetDataParallel( ) which maybe called by many clients currently, and we use the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Jul 8, 2022
in
Education
by
JackTerrance
javamultithreading
0
votes
Q: How to make sure all threads have finished before calling notify?
I'm trying to make sure that all my threads of Class A have finished before notify is called. At ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Apr 17, 2022
in
Education
by
JackTerrance
javamultithreading
0
votes
Q: open() in Python does not create a file if it doesn't exist
What is the best way to open a file as reading/write if it exists, or if it does not, then create it and ... to do the opening part. Select the correct answer from above options...
asked
Jan 30, 2022
in
Education
by
JackTerrance
python
linux
file-io
0
votes
Q: Why does change in temperature doesn’t effect stochastic update?
Why does change in temperature doesn't effect stochastic update? (a) shape landscape depends on the network and ... function which is fixed Please answer the above question....
asked
Aug 30, 2022
in
Education
by
JackTerrance
artificial
intelligence
ai
latest
questions
and
answer
0
votes
Q: Why doesn't this Javascript (jQuery) go into an infinite loop?
I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Mar 9, 2022
in
Education
by
JackTerrance
jqueryloops
0
votes
Q: Why doesn't this Javascript (jQuery) go into an infinite loop?
I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Mar 8, 2022
in
Education
by
JackTerrance
jqueryloops
0
votes
Q: Why doesn't this Javascript (jQuery) go into an infinite loop?
I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Mar 8, 2022
in
Education
by
JackTerrance
jqueryloops
0
votes
Q: Why doesn't this Javascript (jQuery) go into an infinite loop?
I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Mar 6, 2022
in
Education
by
JackTerrance
jqueryloops
0
votes
Q: Why doesn't this Javascript (jQuery) go into an infinite loop?
I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Mar 5, 2022
in
Education
by
JackTerrance
jqueryloops
0
votes
Q: Is there a reason why the default modelbinder doesn't bind to fields?
I'm using ASP.NET MVC3 and i'm wondering that the default modelbinder binds to public properties but not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Feb 11, 2022
in
Education
by
JackTerrance
interview-question-answer
0
votes
Q: Why it doesn't display POST value?
Why it doesn't display POST value? Error: Notice: Undefined index: q[abc] in line: echo $_POST[' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked
Jan 13, 2022
in
Education
by
JackTerrance
interview-question-answer
0
votes
Q: why android devolepor doesn’t use support android library for a large project?
why android devolepor doesn’t use support android library for a large project? Select the correct answer from above options...
asked
Dec 11, 2021
in
Education
by
JackTerrance
computer-science-interview-questions
computer-science-questions
0
votes
Q: <Framework X> does <feature Y> – why doesn’t Django?
does – why doesn’t Django?...
asked
Jul 2, 2021
in
Technology
by
JackTerrance
interview-question-answer
technology-questions-answers
...