Answer the question
In order to leave comments, you need to log in
A banal question on streams
If my worker class inherits from Thread and it has a public method that is called from the main thread, then the method will be executed on the main thread, or what? And if it’s true that basically, then here’s another more specific question: how in this case to avoid the fail-fast behavior of iterators, if I need to add the contents of the second one (which comes from the main thread) to one ArrayList, and at this moment the thread itself is supposed to is engaged in processing evonnogo, ArrayList'a-first content?
In addition to synchronization, nothing comes to mind, but I think it will negatively affect the processing speed (data will often come to workers from the main stream, which is the input), so if there are alternatives, poke your nose, please.
Answer the question
In order to leave comments, you need to log in
If you are not well versed in threads, synchronization, etc. (and judging by the first question it is), then I would advise you to use thread-safe collections from the java.util.concurrent package as an option.
Well, or write synchronization yourself.
ps if the method is explicitly called from the main thread, naturally it will be executed in it. It doesn't matter what class it is declared in.
Perhaps before trying to do something multi-threaded, it's worth learning how multi-threading works? Your questions clearly indicate that you do not know this. I would recommend to get started with the Java Memory Model .
Now for the content: naturally, the method is executed in the thread that called it.
Are you sure that you need to append exactly to the end of the ArrayList? Because the described situation (especially together with the worker keyword) hints that you need a work queue in which the main thread will put and from which the worker will take until it comes time for him to stop. There are many specialized classes in the standard library for this. For example, ConcurrentLinkedQueue or ArrayBlockingQueue .
If you still need to add another array atomically and thread-safely to the end, then look at CopyOnWriteArrayList.html#addAll(java.util.Collection) .
If you don’t like it, do synchronization, don’t be afraid of brakes and don’t prematurely optimize what you don’t understand.
workDoNow is not used anywhere other than the worker thread. Each worker has its own workDoNow, as well as a temporary array. Since neither reading nor writing from other threads is carried out in the workDoNow of a specific thread, I don’t see the need to additionally synchronize it.
Thank you for the last one - an interesting topic, I'll read it at my leisure.
In general, the issue has been resolved, if there are some jambs in the code, then until my bunch of classes grows into an application, I will not find them. Thanks to all who posted above.
UPD: I missed the answer, it's in the thread above.
if there are some jambs in the code, then until my bunch of classes grows into an application, I won’t find them.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question