in Education by
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)

1 Answer

0 votes
by
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
    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 Jun 2, 2022 in Education by JackTerrance
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    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
0 votes
    does – why doesn’t Django?...
asked Jul 2, 2021 in Technology by JackTerrance
...