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.
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
The target computers have either MySQL or SQL Server running on.
We get JDBC drivers for MySQL and SQL Server in our project.
When the users start the application they select which of the 2 databases to use.
One thread handles the GUI CRUD buttons and another handles the CRUD logic.
We get everything into .jar file(including the JDBC drivers) and convert it to .exe to be executed on the target computers.
Is this plan correct or it doesn't work this way?
Just include both the drivers, that's totally ok. As long as you only load one there definitely won't be any problems.
You may implement a simple detection of the databases at their standard paths / check if it is already running on the default port. If there's only one database avaiable, just work with that.
If you use Swing, there's the SwingWorker class to encapsulate (long running) CRUD operations. The gui is managed by the main thread / EDT, there's no need to exactly create 2 threads, if you're doing it right. (Although internally there will be multiple threads, but I'm talking about explicit Thread creation here.) Otherwise you would produce them busy wait scenario which could be really cpu consuming.
You can produce runnable jar files, which can be double clicked and executed on most systems, there's no need to convert it to exe-files.
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 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.
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.