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.
Related
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.
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 5 years ago.
Improve this question
I have a problem I've been stuck on for a day now. I am implementing a test using threads. I cannot post the code here because it's confidential but I can give you the structure:
class Something{
String error = "";
Listener listener = new Listener{
onError{
error = "something";
}
...
create some threads
start threads
The code in each thread may or may not give error, therefor modifying the error. My concern is that, having multiple threads, it will "confuse" the listener.
Attention: the listener is not for the threads themselves (doesn't listen if the thread stop or stuff like that). The listener was implemented independent from the threads but I do need it.
Help is much appreciate it. Ask any questions if not clear.
I assume that by "confuse the listener" you are talking about errors that you may get due to the listener being called from multiple threads. Just make sure that the listener is "thread-safe" and can be called from multiple threads. For instance, a listener that interacts with a variable or structure without locking out other threads may be a problem if multiple threads try to change the same structure concurrently.
If the method only deals with local variables or member/global variables that are locked for concurrency, you should not have an issue. It is hard to provide specific advice without knowing the code which you cannot share.
Search online for "thread-safe" for Java and Android and you should find some useful information. What you are trying to do is not unusual or weird: It is common practice, but you must take care.
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 6 years ago.
Improve this question
I have a web application with Java server, and the Java application is getting requests from the web.
The problem is that there is an instance of an object that can handle only one request each time, and the web requests are coming before the object finishing its job.
How do I create some sort of "connection pool" for these requests? Or is there any other way that I can limit the access to this object instance?
I am using the synchronized declaration at some method, and it is doing a pretty good job, but the problem is that its is not ordered. How can I control the order of the incoming requests for this method?
You can try to synchronize the method that is shared by all the request thread by using synchronized keyword.
The drawback of this is that it will affect your performances.
SO TRY to keep the synchronized block small.
You could consider the requests as commands and store them on a queue and process them 1 by 1. This has however consequences for the user (as already mentioned before) that the response time will increase. To overcome this a bit the handling of the command might be done asynchronously, meaning that the user will invoke the command and get a answer back that the command was received correctly. Once the server is done with handling the command the server can call back the user notifying him that the command is (successfully) processed.
Note that this can become a complex solution and it might be better to check whether you can't remove/refactor the singleton.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I generally write most of my code without worrying about threading and such, and get it working and debugged before trying to offload pieces to other threads. For me, the simplest way to do it is to break the function down into a Runnable or two. From there I can start a piece via new Thread(runnable).start() and other pieces I start on the main thread via handler.post().
The problem is that I can't pass arguments. I can sometimes get around this, but too often I end up using non-local variables, which makes things a real mess. Any ideas on the "correct" way to pass arguments to a runnable?
I usually create a new class implementing Runnable and pass arguments as constructor parameters and store them in final fields.
If I need a result back from computation I implement Callable instead.
I am also using executors rather than threads directly.
Definitely a public synchronized queue.
Create a global queue of data to process, in order, and then use that queue to access external variables/data. Create a method to add data to the queue, and then one to get the last element and shift it.
This ensures that the data gets across to your thread from an external thread, such as the main program thread, without interrupting the program flow.
The queue can be processed by adding value when data needs to be passed, and the get last element method can be called to get the last date element passed to the thread.
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 got a problem, lets say i have got code like that:
public static void main(String[] args)
{
startMethod1();
startMethod2();
}
Now, if startMethod1 method is very big and takes a lot of time to execute it, startMethod2 is started before startMethod1 has been finished.
How to make those methods execute 1 by 1?
startMethod2 is started before startMethod1 has been finished.
This will never happen. You can start threads in startMethod1 which could still be running as startmethod2 starts. startMethod2 cannot start before startMethod1 unless startMethod1 calls startMethod2
If you want to wait until the threads in startmethod1 have finished you have to Thread.join() them or use ExecutorService.awaitTermination()
How to make those methods execute 1 by 1?
Just as you have written the code.
They already do. The way you've written it, startMethod2 will not start until startMethod1 runs.
If you are using Threads then please change your code-example... but i pretend that you did, because your comment on another question showed that you meant to ;-)
You might take a look at wait() and notify()
Call wait() to tell the current Thread to suspend, and notify() to wake it up again
My guess is that you're using some kind of message logging API which is buffering, and hence some output is appearing much later than you expect. Possibly not even flushing until the end of the program. Hence you're getting interleaved output which is misleading you.