Need of SingleThreadPool in Java? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a few questions regarding ExecutorService.
In what cases we should use newSingleThreadExecutor() than others and Why?
Can you tell me the real use case of having SnewSingleThreadExecutor()?
If we have a single thread either from (newSingleThreadExecutor() or newFixedThreadPool(1) or newCacheThreadPool(1)) Do we still need to check for Thread Safety?
Why do we need newSingleThreadExecutor() if we can already create a single thread using newFixedThreadPool(1)

When you don't want tasks to run in parallel because of common data.
Swing's Event Dispatch Thread. It is not called executor, but in fact it is, just its execute method is called invokeLater.
It depends on what data you access. If that data can be accessed outside the tasks running on this executor, then yes. It does not depend of how you built your executor.
We do not need. I don't know what SingleThreadPool do you mean - there is no such class in Java runtime libarary.

Related

Safely edit the Database with multiple threads in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to learn multi-threading in java.
Suppose I have a very big application with 100 running threads trying to execute a synchronized method which inserts a row in database.
As, the method is synchronized so only one thread will get the lock for that method and rest 99 will wait.
Only 1 thread is able to edit the Database and rest will be waiting. It seems a slow process. As all the threads will be editing the DB one by one. Is there any other way or concept to safely edit the database in a faster way?
i will recommend u to read about isolation level in transaction to handle some cases in concurrent application https://en.wikipedia.org/wiki/Isolation_(database_systems), sometimes is handles by default.
if for isntance u only adding new rows in table u shouldn't care about it and remove synchronized

which are mandatory activity of start() method in java thread class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that why you have to call start method in thread but i want to know that what are other mandatory activity done in start method except calling run method..
You are asking what the start() method does?
Basically ...
It creates a thread stack for the thread (typically with a "red-zone" to trap stack overflows)
It creates / starts the native thread, passing it the Thread object.
It returns to the caller.
Meanwhile, the newly created / started native thread calls back to the JVM to run the run() method.
(A bunch of other important things happen in the instantiation of the Java Thread object. Before start() is called.)
If you want more detail, please refer to the OpenJDK source code.

implement thread in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to maintain thread pool, and it should contain multiple jobs. Job has execute one after another
If you have a sequential execution of four jobs, you probably want to aggregate them into a single Runnable.
void run(){
job4(job3(job2(job1(inputs))));
}
It does not make much sense to schedule separate threads for the jobs (as only one of them can proceed at the same time).
You could submit that whole thing as one to an ExecutorService.

Java Executor to execute one Callable after previous has finished? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a custom business requirement. I need to execute a series of service calls. Each of these call creates a new record in the database. I need to execute the next service call only after the previous service call has finished, because the new service call will use the record created in the DB by previous service call, as a input parameter.
I had decided to wrap each of these service calls in a Future Tasks and then use Executors.newSingleThreadExecutor() to execute these tasks in a loop.
Please suggest will it suffice and a better solution if it will not ??
Also do I need to place task.isDone() before executing the next task??
I have decided to use Executors.newSingleThreadExecutor().
Good choice given your requirement.
Please suggest will it suffice and a better solution if it will not ??
Yes, pretty much so. With the standard JDK libraries, this is probably the best you can find.
Also do I need to place task.isDone() before executing the next task??
No. The javadoc for this executor explicitly states that "[...]Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time."
Therefore, no task will be initiated before the previously active one terminates (successfully or not). Just stuff them into the executor, it will handle things for you.

Questions from Effective Java book(Joshua Bloch) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I started reading Effective Java(Joshua Bloch) and I'm currently at Chapter 3.
Each instance of the class is inherently unique. This is true for classes that
represent active entities rather than values, such as Thread.
I don't understand which this is particularly mentioned the Thread class.
I would be glad if you can help
The Thread class is a prime example of a class that represents an active entities. Each thread is a "thread of execution of a program" (Oracle Docs). In other words, the Thread class models an active computation. It would not make much sense to treat a thread as a value. Two threads instantiated with the same initial state may act differently. That is why multithreading bugs, like deadlock, only sometimes occur.

Categories

Resources