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 8 years ago.
Improve this question
I read lot of blogs and articles that ask to take care while coding for synchronization. They ask to use concurrentHashMap, synchronizedList etc.
As per my understanding, in java web application, Application server (e.g. jboss, weblogic, tomcat), every request run under a separate thread.
e.g. I have sequence of method execution method1--> method2--> method3, then every request will have its own execution stack. Then why do we need to think more about synchronization?
Either my understanding about concurrent request is not correct or I am missing something about synchronization scenarios.
Please advise.
Because in most applications, processing a request involves accessing some shared data that may also be accessed by other threads handling other requests at the same time.
HTTP sessions are a prime example: all requests in the same session share the same HttpSession object, so if a browser sends two requests at the same time, the two threads handling those requests may try to access the same HttpSession object at the same time. You need synchronization to avoid corrupting the session.
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 4 years ago.
Improve this question
In an enterprise back-end Java application, I have a requirement where some part of code calls various API which are independent of each other. On receiving response, I am utilizing them and passing data to requester.
For this, I have implemented callable based multi-threading (using Executor). But a colleague is stating that implementing multi-threading would make my code responsible for managing resources and not the Web App Container which can lead to performance issues.
So I wanted to know, what is the impact of implementing multi-threading in my code? And how can I make sure that resources are managed properly without impacting overall application.
There are a some different aspectes mixed together in your question. Creating threads on an application server is not prohibited because it could cause performnce issues. It's more that the server itself is responsible to manage the system resources. Spawning own threads, of which the server is unaware of, can not be managed by the server. See this page for more info about the topic.
Using an thread-executor that is provided by the platform, is very valid an could be used to implement multi-threading nevertheless. See here for example.
Another aspect of multi-threading is indeed performance. Creating threads comes with a certain cost and creating too many of them may lead to an overhead in conext-switching. The trade-off between pralellism and having to manage a lot of threads has to be consireded by the developer. Again this is why application servers, manage their own thread-pools.
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 8 years ago.
Improve this question
Scenario is that there are 15 web services which I need to invoke to invoke and the consolidated response needs to be send to another system .My query is these 15 services need to invoked for around 1000 requests(because there are 1000 sales people whose data is fetched from these 15 web services). What would be efficient implementation of this . If I create several threads for different sales people then might the services which I am invoking are not thread safe . So should i call the services sequentially ? But that would degrade the performance .
Web services, by their nature, are meant to handle concurrent requests. You only need to worry about thread safety within your client application and can safely assume the web services you are calling are thread safe.
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 9 years ago.
Improve this question
I'm attempting to build a Java Servlet task that has a runtime of about ~15-20 minutes that takes arguments from a HTML form. I have a couple of questions regarding this:
Will the task continue to run even after the user closes the browser? I Googled this and it seems the process will continue to execute even after browser close. I just want to confirm this.
While searching for the answer for the above question, I came across a post (or a couple of them) that stated that for such 'intensive' (I would consider mine intensive as it takes around 15-20 minutes to complete) tasks, it's better to have a separate program run the task than containing it in the servlet program. So, do I just execute another Java program from the servlet class?
And now for my final question, will multiple user requests be processed independent of each other? As in, will the servlet have a separate thread or instance for each request? If so, will my execution of another Java program from the servlet class lead to any problems?
There are a few items to discuss, each with their own (part of a) solution:
Do you really want the task to continue if the browser closes? Spawn a new thread for the task (Trying to write to the browser outputstream when browser is already closed will make the thread die in an exception) See Executor
Do you want concurrent requests to be handled in parallel? How many in parallel? See ThreadPoolExecutor
Do you want feedback to the browser (user) during the long running task? See Async servlets
The servlet container will make sure that parallel requests are handled concurrently, each in their own thread. But they will share the instance of the Servlet class. Therefore, you have to make your code thread safe.
About running a 'separate java program' or keeping the task in the servlet: it is best practice to separate different tasks in a program in different sections. Creating a new class for your long running task is better than keeping it in the servlet class. See Separation of concerns.
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 8 years ago.
Improve this question
Assuming that launching threads from a servlet is something not recommended, I'd like to know what can make this thread, created and launched from a servlet, to stop unexpectedly.
For example, reuse of the servlet thread can cause the child thread to be destroyed?.
I need clear arguments showing that the life of a thread from a servlet generated is not assured to the end, or reasoning to support otherwise.
I do not want a discussion, I want clear and definitive examples.
Threads created from a servlet will not be terminated by the container (as long as the container is running), because of two reasons:
The container does not have references to the threads created inside the servlets, therefore it cannot invoke e.g. Thread.stop() on them (Yes, this is a deprecated method and definitely not a good way of terminating threads, more details).
Only the thread itself can know how to properly unlock the monitors that it has locked so forcing a thread termination will result in inconsistent state of the monitors.
Notes:
This behavior is not covered by the Servlet specification so there is no guarantee how it will work on your web container.
Servlets should not be spawning threads as this is the resource managed by the web container.
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 2 years ago.
Improve this question
I need to build some processes to form a distributed system.
I am in a dillema between RMI and JMS.
Issues:
I opted RMI since I already know it and it fits the distributed systems and it is fast. But the problem is that it is blocking.
I.e. if one of the other process hangs the calling process will be "stuck" on the method call. I think there are some third party libraries but I don't know if they are stable enough.
JMS is a standard and avoids the problem since it is asynchronous. But going this way I have the following issue (also I haven't used JMS before):
If I send a message to one of the processes, I sometimes (depending on the context/flow) need to know that the other process actually did something after receiving my message. But this forms a "synchronous" model, right?
So taking all these into account, what would be the best approach and how my problems would be solved in each case? E.g. my problem with JMS how would it be solved?
JMS is a better solution because of the reasons you mentioned.
Asynchronous
Non Blocking
For receiving acknowledgements you could have the receiver send you messages post some action.
The Actor model which builds on message processing concepts is worth mentioning here.