I have a multithreaded application and I assign a unique name to each thread through setName() property.
Now, I want functionality to get access to the threads directly with their corresponding name to stop it.
How can i get that?
To find a thread you use this: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ThreadUtils.html (https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/ThreadUtils.java)
But that gives you only a reference to the thread and you cannot simply terminate it (stop() is deprecated). Depending on what the Thread is doing maybe interrupting it is an option?
I assume you are trying to get to the thread (by its name) to call Thread.stop() on it. If that is the case - don't do that. The method is deprecated - see why.
This question has some suggestions on how to properly stop a thread.
If you really want to access a thread by name and you don't have a reference that can be used, you could use ThreadGroup and search the tree formed by groups and threads for the one with the correct name.
From the JavaDoc:
A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.
Thus you should be able to call Thread.currentThread().getThreadGroup(), use getParent() to find the initial/root group, list all active threads using enumerate(Thread[]) and search the threads.
Related
There is method java.lang.ThreadGroup.activeGroupCount() returns an estimate of the number of active groups in a thread group. In response to this question, the active thread is defined. But what does active thread group mean?
As you noted, the terminology "active thread group" appears in the javadoc for ThreadGroup::activeGroupCount.
An active thread group is a ThreadGroup containing at least one active thread.
An active thread is one for which Thread::isAlive returns true. In other words, it has been started and has not yet terminated.
Note that thread groups are are only really suitable for debugging; see What is the benefit of ThreadGroup in java over creating separate threads?. For example, the enumerate method has this javadoc caveat:
"Due to the inherent race condition in this method, it is recommended
that the method only be used for debugging and monitoring purposes."
This also applies to the "count" methods.
In Java there is a group abstraction around a group of threads, so it is easier to manage a group of threads. See e.g. Java: Thread Group
Every Java thread is a member of a thread group. Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually.
For example, you can start or suspend all the threads within a group with a single method call. Java thread groups are implemented by the ThreadGroup(in the API reference documentation) class in the java.lang package.
I was wondering, what are the advantages of assigning threads to a thread group instead of containing them all in one (The Main) group?
Assuming there are 10 or more constantly active threads, and a couple of threads been initiated every now and again as the application requires, how would one approach grouping these?
Thanks,
Adam.
There is no advantage at all. ThreadGroups are there for backward compatibility, but I've never seen them used.
Here's what Brian Goetz (author of Java Concurrency in Practice - the bible) said about them a long time ago:
The ThreadGroup class was originally intended to be useful in
structuring collectionsof threads into groups. However, it turns out
that ThreadGroup is not all that useful. You are better off simply
using the equivalent methods in Thread. ThreadGroup does offer one
useful feature not (yet) present in Thread: the uncaughtException()
method. When a thread within a thread group exits becauseit threw an
uncaught exception, the ThreadGroup.uncaughtException() method
is called. This gives you an opportunity to shut down the system, write
a message to a log file, or restart a failed service.
Threads now have an uncauht exception handler, and this single reason to use thread groups isn't valid anymore.
I want to know how to replace current thread of execution with a new thread object in Java. Some more context, the new thread will be fetched from a concurrent hashmap of with unique key and value as thread objects . Unique key might be obtained from the current thread of execution.
You can't really "replace" the current thread with another thread, you can just add another thread and terminate the fist one. I don't know if you consider that replacing.
If you show us some code demonstrating how your "current" thread is being started and approximately what it's doing, then we might be able to give you some more pointers on how to either block the current thread (thus forcing it not to work) or to terminate that thread all-together (which are pretty much your two major options).
I'm using the executor plugin and there are several jobs executing, however I want to identify each one and stop the job that I selected, is that possible?
I am not sure if there is an easier way but you could create a thread group for all of your executors and make the group a container of all your async tasks (executions). Then instead of passing a closure to the submit method of the plugin, you would create a named thread inside the ThreadGroup and pass the Thread to the submit. Make the TG being part of your spring context or make it global in one way or another. This way having a reference to the thread group you could get back to any active thread (and by extension to any async executions) by doing an enumerate on your TG and by inspecting the threads enumerated you could get to the thread's name, identifying any particular thread you want. You could then interrupt the thread.
I want to have a while loop that launches a thread on each loop,
I am using the following to launch the thread, do I need to have a unique identifier for each thread or becuase it is launching from different loops will it launch ok, or will it overwrite the previous launch as they are using the same identifier?
while(x<y){
Runnable r = new Rule1("neil", 2, 0);
new Thread(r).start();
x++;
}
It will work fine.
Your threads do not have any identifiers at all.
The r variable is a normal (and temporary) variable; you are passing its value to the Thread constructor.
The runtime isn't even aware of the variable.
It will launch multiple threads. The only "unique identifier" for the thread is the ID returned by Thread.getId(), and you don't get to assign that. Even the thread name doesn't need to be unique. In other words, there's no unique identifier which is being reused here.
Certainly the fact that you're assigning the Thread reference to the same variable on each iteration doesn't mean the threads will care in the slightest.
While you do not need to create a unique identifier for each thread (framework will take care of it) it is best practice if at all possible to name your threads in relation to what they are doing so that when you begin debugging via logs/jvisualvm you are aware of the threads purpose.