I have a thread which send request to server and process request from the server.
I have a trouble, when i use CountDownLatch in my thread.
What the trouble?
When i send request i do latch.await() later, and try to latch.countDown() after request was obtained from the server but i think when response come it was not called in listener because main thread is blocked.
I will explain my problem with picture below :
So how can i fix this or maybe exist another way to handle this problem?
First of all, blocking the Main thread is a really bad practice because your UI becomes unresponsive and you should definitely try to avoid that.
A simple solution might be to use an AsyncTask and if you read the documentation it does handle the execution flow for you, passing the results from a worker thread to the UI one.
If you don't need a result that affects directly your device UI, you can use an IntentService.
There are definitely other solutions available(even with Latches) but a general rule should be:
avoid heavy operations on the Main Thread
avoid blocking operations on the Main Thread
Related
This example shows how to use the async methods of Twitter4j to make requests to the Twitter API, but it only makes one request.
I want to be able to fire multiple queries to the API, execute them asynchronously and wait for them to finish before exiting the program.
Since I'm not creating the threads myself, I'm not sure how I can create the synchronization mechanism. I thought about creating some kind of thread counter, and decrementing it on the callback listeners, but seemed a little hackish.
Any help? Thanks.
this might be what you are looking for https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
For every request, there is a lot of computation happens. On an average the reply takes about 10 minutes to process. Now in the mean time, if a user sends a new request; There is absolutely no point for the previous request to continue.
So I have written a code where I basically interrupt the previous thread executing it. Is it a good practice in tomcat environment? Can there be a better solution to deal with it. Is it alright to interrupt tomcat threads.
Or should I manage my own threadpool and let pool do the computation for me?
More Information:
Basically the whole task is wrapped with a FutureTask. For every request, this task is executed and the reference to the task is stored by a ConcurrentHashMap. For every request, all the future's in the map is "cancelled" and then proceeds to execute the latest request. Thus cancelling the previous requests.
Q> I basically interrupt the previous thread executing it. Is it a good practice in tomcat environment?
A> I think it's fine as long as you're happy having HTTP thread(s) blocked for 10 minutes. This means no other user would be able to process HTTP requests. Otherwise create your own thread pool and manage it.
Q> Is it a good practice in tomcat environment?
A> Interrupting Runnables or Callables can be tricky. For example, if your thread is in the middle of I/O operation, interrupting can leave the data in a corrupt state. Other than that, this is quite normal practice. I also recommend using your own thread pool in order for your server capacity to be predictable.
Can you break your large task into a lot of smaller tasks? Sticking a conditional and exiting early could be a good alternative to interruption.
Alternatively, does waiting for/ensuring the first task finishes the operation and others just return the same value make sense in your environment? If so, I'd rather prefer that instead of your approach. There's LoadingCache from guava library which does exactly that.
I am not sure I understand your question completely...
But, if you are talking of running a threadpool within a tomcat application and cancelling its future tasks, I see no problem with that.
I would not interrupt a thread allocated by tomcat, unless I would write code to deal with the interrupt personally (such as within the servlet class)
I have a check box. If I checked it then I need to upload data to server. I have written this uploading logic in a Thread class. If I uncheck the checkbox I want to cancel the uploading and so on. The problem is that while trying to use the same thread if in some situation I am getting thread state as TERMINATED (this.getState() == Thread.State.TERMINATED). What I need to do in this case? If thread state is new then I will call start() method. But in this case what do I need to do?
Simple answer: do not reuse thread. Thread pooling has its advantages on big serverside sysems, but not on mobile phone there is no advantage.
Terminated thread is dead, no action is necessary on your side anymore
Use can try to use AsyncTask instead of thread.
http://developer.android.com/reference/android/os/AsyncTask.html
I have a java application which calls a third party method, which can block indefinitely without throwing an exception.
Is it possible to wrap my method call in a timeout block (or thread, or other construct) such that I get back control after I assume the call is never returning?
The ThreadPoolExecutor should do what you need. Using the awaitTermination method:
Blocks until all tasks have completed execution after a shutdown
request, or the timeout occurs, or the current thread is interrupted,
whichever happens first.
all this multi threading is surely an answer but think if ur application is not a multi-threaded one, you can just store the timestamp of the moment you send the request and check it against the current timestamp. of course you will need a thread to keep track of the time. but all in all you can use that same thread for this purpose for as many functions calls you need. so dont go on implementing the runnable in ur classes. just make one tracker thread.
I'm looking at a spring #Controller method that calls an #Async method, does some "other stuff", then calls .get() on the Future returned by the async method call.
I can see how this may improve the response time of the controller as both the async method and "other stuff" involves calling web services and sure, why not execute multiple ws calls in parallel. But to do this inside inside a controller seems, well, unusual for me.
I've always thought that messing around with threads in a web app isn't such a good idea, after all web apps are run on servers with their own ideas about threading (and a fixed thread pool for handling requests).
Does this have a bad smell about it? Am I worrying about nothing? How will spring handle the concurrent threads, by creating a new thread outside of the servers control or by using another thread from the server's pool?
This doesn't sound like the best approach. Kicking off an async task to get a Future, then simply calling Future.get in the requesting thread means you've got two threads occupied - one doing the work for the async task and one waiting for that work to complete.
If something happens on the requesting thread in between the async task starting and the call to get, then I can understand this. Otherwise, it's better to simply make a synchronous call.