Содержание
Find thread by name java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
[Solved]-Get Thread By Name-android-Java
You can find all active threads using ThreadGroup:
- Get your current thread’s group
- Work your way up the threadgroup hierarchy by calling ThreadGroup.getParent() until you find a group with a null parent.
- Call ThreadGroup.enumerate() to find all threads on the system.
The value of doing this completely escapes me . what will you possibly do with a named thread? Unless you’re subclassing Thread when you should be implementing Runnable (which is sloppy programming to start with).
That’s how I did it on the basis of this:
/* MIGHT THROW NULL POINTER */ Thread getThreadByName(String name) < // Get current Thread Group ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); ThreadGroup parentThreadGroup; while ((parentThreadGroup = threadGroup.getParent()) != null) < threadGroup = parentThreadGroup; >// List all active Threads final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); int nAllocated = threadMXBean.getThreadCount(); int n = 0; Thread[] threads; do < nAllocated *= 2; threads = new Thread[nAllocated]; n = threadGroup.enumerate(threads, true); >while (n == nAllocated); threads = Arrays.copyOf(threads, n); // Get Thread by name for (Thread thread : threads) < System.out.println(thread.getName()); if (thread.getName().equals(name)) < return thread; >> return null; >
I like the HashMap idea best, but if you want to keep the Set, you can iterate over the Set, rather than going through the setup of converting to an array:
Iterator i = threadSet.iterator(); while(i.hasNext()) < Thread t = i.next(); if(t.getName().equals(threadName)) return t; >return null;
Pete B. 3112
An iteration of Pete’s answer..
public Thread getThreadByName(String threadName) < for (Thread t : Thread.getAllStackTraces().keySet()) < if (t.getName().equals(threadName)) return t; >return null; >
VicVu 6883
Related Query
- Can I get the name of the class and method within which the current thread was spawned?
- How to get AsyncTask’s thread name before execute it?
- How to get an object identifier for a hash algorithm name in Java
- Get caller class and method name
- How to get Spring profile name from annotation?
- How to get the category name of the character type in Java?
- How can I get the javadoc of all methods with a specific Annotation, with the method name and package/class name?
- How to get the wifi network interface name in java
- Get Active Directory Domain Name For Current User Using Mac OSX
- How I can get the contact name from the android (CallLog.Calls.CONTENT_URI) table?
- How to get thread ID in Java
- Is it possible to get the class name from a static method?
- TestNG — How to get current class name from BeforeClass
- How to get activity title name through programmatically java android
- Get name of day from timestamp in Android
- How to get document id or name in Android in Firestore db for passing on to another activity?
- Get the currently used protocol name from HttpServletRequest?
- How to get the current class name in Java with log4j
- java Get the list or name of all attributes in a XML element
- Why do you have to use .class in Java to get the Class object? Why not just the class name like in Ruby?
- How to get a SubClass Instance, when i know the Class name as String in java?
- Could I get the name of current reference within Java?
- How to get czech name of month
- Is there any way to get job keys in Quartz by job name
- How to get class name of any java file
- Android — Get the original file name for an async download
- How to get a thread submitted to executer service and interrupt it in Java
- java get name of package of class
- Is there a handy way to get the name of a Spring Bean/Service?
- How to get carrier name from dual sim phone Android?
More Query from same tag
- Design pattern for managing groups of tasks
- Request.getAttribute(«javax.servlet.request.X509Certificate») Returns Null
- How do I define a generic member variable in an enum?
- Simple java ticketing system in netbeans
- Factorial loop results are incorrect after the 5th iteration
- For a particular method, how can I view the checked exceptions in java using reflection?
- Exporting database to csv file with java
- Runtime Exception using Glide Image Load setDataSource failed: status =
- JAAS Realm load internal config file
- Wildfly 12 & 13 duplicate standalone.xml file and log it as an infinite loop
- How to close popup window?
- JavaFX StackPane bounds not updated
- Implementing pingpong game correctly
- How to change from for to foreach in this case?
- Spring aspect call on custom annotation on interface method
- Regarding synchronized methods in Java
- Hibernate/JPA Parent-Child — is it okay for Parent equals()/hashCode() to use the DB Id?
- IllegalStateException: Can’t overwrite cause in Guava map.put
- No suitable driver found for jdbc:jtds:sqlserver
- Amazon Alexa: AMAZON.DATE to Java Date/Duration
- MouseListener on a drawString() Method
- Bitwise and (&) operator
- Extending/Inheriting Tomcat Projects
- I can’t get a ListView to display the names of files in a folder
- Twitter4J verify Credentials and catching error
- Java: Passing Subclass Type as Parameter of Main()
- In spring security oauth, What is the default duration of refresh token?
- Having a class itself as its constructor’s only parameter
- Java — How to pass XML request body in rest post method
- How to rotate an image based on two points