java - What order do threads run (execute) after being submitted to ExecutorService? -


There is a better way of writing files to safeguard (in cases where files can not be all the files Or compared to syncing the file author in each thread) I read some threads similar to this topic, but they start focusing on a file in opposition to multiple files.

Ex There are 20 threads that write (which means that it uses a method that creates the file author in the file and then files it with a try-hold, etc. Writes for); Let's write 10 files of thread, write 5 threads fileB, write 4 threads on fileC, and write 1 thread in D.D.

By synchronizing the method, threads that want to be written in separate files, before proceeding with it, to wait for the previous thread to finish it, I think synchronizing the file author is very similar. Or am I wrong?

If I have a separate thread thread (from the main application) that writes in a file, then do they execute in the order submitted to the executable service with 1 thread (run) Do you?

In the main application, I will submit new threads to the Executors service (Uses 1 thread). Threads write in a file (using a method of writing that the fireware is synchronized with a logger class). Threads will write the file one by one because FileWriter has been synchronized and there are only 1 threads for the executable service, which will prevent multiple typing in the same file at once. The question is whether the threads will be written in the file which was assigned to the executors service? I know that the orders that were submitted were started, but I am also not sure about the execution order.

You are mixing a few things that create confusion: first, executable service is an interface that is not required in a particular way how the submitted work ( no threads ) is executed. Therefore, it does not make sense to understand that a executable service will do as a special thing, because it is not specified. It can also quit all tasks without executing anything.

Second, as mentioned earlier, you are not submitting jobs , with thread, ExecutorService tasks runable You can apply or callable .

Unfortunately Java has a design flaw that implements threads Runnable so you actually get the threads example Submit () , which you should never do, because it does not make any profit. When you do this, the Common Executor Service implementation will treat it as a normal Runnable its run () method completely Ignoring that this is a thread example. The thread processing related to that threads example, in fact, the run () method ( if executes the call anytime) code> run < / Code>).

So if you submit ExecutorService implemented task as code Runableable or collage code> about implementing you To know, special implementation documents must be studied.

Example If you use to get the implementation, its documentation says:

Creates an executable that uses a single worker thread with an infinite queue (Note that if this single thread ends due to failure during execution before the shutdown, then a new one will take its place, if necessary, then the work is executed.) Work order It is guaranteed to execute, and more than one task will be activated at any time

(Thrust by me)

So answer your question completely. Note that in this case you do not need synchronized , as this executable service provides the necessary mutual exclusion guarantee for your actions.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -