Occurs before the java thread starts

I read somewhere that starting a thread has some special effects on events before the relationship Now I'm not sure if my code guarantees this before the relationship, so please let me know

I have a dispatcher thread and a worker class that implements the runnable interface The dispatcher thread creates a new instance of the worker and populates the LinkedList in the worker instance through the add method with elements

Then, the dispatcher passes the worker instance to executorservice. Com through the execute method

Then, the run method in the worker class starts accessing and deleting things from the LinkedList

Does the newly generated worker instance see the same LinkedList status as the dispatcher? Or maybe the LinkedList is in some inconsistent state? Do I have to populate the LinkedList in the synchronization method?

Solution

Java language specification writes:

However, according to your description, when you talk about executing programs, it is not clear whether this is relevant to your situation, but it does not explain when to create an executing program or start its worker thread

Related are the following starting from the executor's Javadoc:

Therefore, your code is safe as long as the scheduler thread no longer accesses the list after submitting runnable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>