Stop running thread group in java - java

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?

Related

How to kill a thread running in the background - Java?

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

How to kill a thread in java?

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.

How to start 1K threads and continously run the threads on the same task when they complete

If I create 1K threads and launch them at the same time using a latch, once the threads complete my process ends.
What I want to do is, as the thread ends, start up another thread to work on the same task (or somehow get the same thread to continue processing with the same task again).
Scenario:
I want to start 1K threads, and don't want the performance penalty of starting another 1K threads when they finish processing.
The threads simply make a http url connection to a url http://www.example.com/some/page
What I want to do is continuously run for x seconds, and always have 1K threads running.
I don't want to use an executor for this for both learning how to do it w/o it and I believe since the executor framework separates the task and threads, it doesn't gaurantee how many threads are running at the same time.
You'll have to do it in the Runnable itself. Create a simple loop surrounding your actions.
If you want them all to synchronize at a certain point, create a CountdownLatch with count 1000 and at the end of every iteration do a countDown and await.
Apache JMeter is a free performance testing tool that you can easily configure to test URL's in multiple threads. It can also distribute the tests to have e.g. 10 clients doing 100 threads instead.
use a loop in your run() method.
Close as I can tell, you want to have a large number of server threads, and have them execute a piece of work from a list, then come back and wait for another piece of work to be be specified (or work on another already-present piece in the list).
This is what you use a queue for. Probably a BlockingQueue is the simplest form to use that will suit your purposes, and there are several implementations of this in the JDK.

In Java if one thread got killed, what will happen to the other thread?

I want to know in Java:
If main thread got killed what will happen to other children threads?
If child thread got killed what will happen to siblings and parent thread?
I read in the following link that since threads sharing address space, killing one thread can affect other thread also.
Below is a quote from here.
Threads are light weight processes that divide main flow of control into multiple flows and each flow of control/thread will execute independently. Activity of the process in a system is represented by threads. The process that has multiple threads is called as multi threaded. Each threads has its own thread ID ( Data Type Integer), register, program counter, stack, error no. Threads can communicate using shared memory within same process.
There are different advantages of using threads to mange and maintain the subtask of applications. When we are using threads than less system resources are used for context switching and increased the throughput of application. Threads also simplify the structure of program. There is no special mechanism for communication between tasks.
Threads also have some disadvantages for example threads are not reusable as they are dependent on a process and cannot be separated from the process. Threads are not isolated as they don't have their own address space. The error cause by the thread can kill the entire process or program because that error affects the entire memory space of all threads use in that process or program. Due to the shared resources by the threads with in the process can also affect the whole process or program when a resource damage by the thread. For concurrent read and write access to the memory thread will required synchronizations. Data of the process can easily damage by the thread through data race because all the threads with in the process have write access to same piece of data.
Can u gys please tell whether whatever told in the above link is applicable to java
1) Nothing will happen to the "child threads"...
2) Nothing will happen to the "sibling threads"...
...with the following exception: If all remaining threads are daemon threads, the application will terminate (i.e., when there are only daemon threads left, these will be killed as well).
From the documentation of Thread:
[...] The Java Virtual Machine continues to execute threads until either of the following occurs:
The exit method of class Runtime has been called [...]
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
Nothing, in both cases. Threads run independantly of one another and there's no such thing as "parent" or "child" threads in this sense. The process will continue to run until there are no threads running in it.
A process is simply a container that contains some threads. The threads execute code. If there is one thread or more running inside a process container, the process will continue to exist. There's no symbiotic relationship between the threads, killing one will not kill another.

java: New to threads. Is this possible?

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"

Categories

Resources