I have situation where for every hit from a user a thread from a thread pool will be running in the background. So when multiple users hits there will be multiple threads running in the background. Now when one user refreshes their browser I want to kill that thread running the particular user's browser window so that the thread goes back to the thread pool.
Is this possible? How can I do it?
Thanks in advance
Hopefully this helps.
{jstack JAVA_PID}
Gives you list of threads with process id and thread id which are run in the jvm.
The thread id is hexa format. Map this output to top command which is mentioned below.
top JAVA_PID
Related
In APM pinpoint, I can see all http calls threads for Java sample application on "Real time active thread chart". But i cannot find out any background running thread which internal threads. Is there any way to display those threads in chart?
Very late reply, but just in case, could you try adding your background service to
profiler.entrypoint in pinpoint.config
e.g. profiler.entrypoint=com.example.background.ClassName.methodName
I have a processMedia method which takes like 1 mint and multiple threads hits the same method when ever user refresh the page.
So after any of the running thread finish process that media, I want to stop all the threads which running for process that media.
So I need to kind of map all threads with media id and stops them. How can I do it in proper way?
This question already has an answer here:
JavaFX2: Can I pause a background Task / Service?
(1 answer)
Closed 8 years ago.
A worker thread is used to process a very long-running task. At the middle of task execution, a user interaction is needed to acquire inputs. As the inputs are supplied, the worker thread will resume execution. My problem is, the worker thread needs to be suspended, get inputs (shall we say through a dialog box - I am using the Dialogs API of JDK8u40 which must be facilitated by the JavaFX App Thread), and resume thereafter. The inputs are not supplied at the start due to dependency of certain situations, and inputs might be needed many times.
A typical example is, files are being copied from one directory to another. As the files are copied, a file with the same file name exists in the destination directory, thus a user interaction is needed to rename the file or skip file copying to avoid file name conflict. In this scenario, the worker thread is supposed to execute file copying, and is needed to be suspended to acquire inputs from user (must be facilitated by the JavaFX App Thread) before proceeding. Platform.runlater(() -> {}); can't do this kind of situation, for it would just queued the Runnable object to be ran at some time in the future.
How to facilitate this scenario? I am new to JavaFX concurrency.
You can use the wait notify mechanism as described here: http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html.
When an interaction is needed a runnable is launched through ui thread to prompt the user. The worker thread calls wait. The ui thread eventually gets the user input and notifies the working thread which continues his job according to the message it receives. The difference here is that you do not need to create two threads as the ui thread already exists.
I have jsp page which starts a thread whenever user logins on that page .But what I want is that only a single instance of thread must run i.e. even if user logins for n times on that page only one thread should run.currently whenever user logins a new thread get created which I don't want.For this Either I have to check that one thread is already running or not and if already running then I won't start another, or I can kill the thread which is already running and start a new one.Now ,the problem is How can I do this?I am new to java programming so I don't know my options?
Check out if the user already has a session and if he does, don't start the thread.
In fact if the user already has a session, don't let him go to the login page, go to the front page instead, then he can log out if he wants and log in again.
Why not just check oldThread.isAlive()? and stop spawning a new thread if this returns true.
BTW you should use ExecutorService for such things.
Also, you cannot actually kill a thread in Java. When a thread's execution finishes, its state will be terminated.
I'll try to be short.
Need a number of threads to open sockets (each thread opens one socket) and make HTTP Requests. I am new to multi-threaded and I don't know if this is possible, since each thread must be running until the request is finished (i think).
[edit after comments]
I don't know if this is possible since currently running thread can be suspended before the response is fetched.
Thanks for any help.
It sounds like a Thread pool is what you need.
There is a section in the Java Concurrency Tutorial about them.
(This is pretty heavy stuff for a beginner though)
Yep, definately possible.
In response to your further query
The fact that a thread is suspended doesn't stop it from recieving data over a socket. If any data arrives while the thread is suspended it is queued until the thread resumes.
What do you mean by "suspended"? If you refer to the context-switching between threads, then you have some holes in your understanding of multi threading. It is the same as multi tasking in a OS: You're running Word and Explorer at the same time on your machine, and the one application doesn't die when the other needs to run - the operating system instead puts one process/thread into wait by saving all its state, then retrieves all state for the next thread and then sets it into motion. This goes back and forth so fast that it seems like they run at the same time - but on a single-processor machine, only one thread really runs at any specific time.
The thread itself doesn't "know" this - only if it continuously run in a tight loop checking the time, it will notice that the time jerks: The time increases smoothly for some milliseconds, but then suddenly the time jumps forward and then still runs smoothly for a new set of milliseconds. The jump is when another thread was running. Each such period of smooth running is called a time slice, or quantum. But if the thread doesn't need the processor, e.g. when it waits for I/O, then the OS takes it back before the time slice is over.
The thread exits (dies) when you exit/return from the run() method - not before.
For fetching multiple HTTP connections, multi threading is ideal: The thread will use most of the time waiting for incoming bytes on the network - and while it waits, the OS knows this and sticks the thread into "IO wait", instead running other threads in the mean time (or just wastes away cycles if no thread needs to run, e.g. everyone is waiting for IO - or in these days, the processor throttles down).
Yes, what you describe is very typical amongst java programs that retrieve data via HTTP.
Yes, this is possible.
Look here: http://andreas-hess.info/programming/webcrawler/index.html
or google for "java multi thread web crawler"