Java 访问自定义线程方法
我用额外的方法创建了一个自定义线程。我在循环中创建了几个线程 我想知道是否可以使用Thread.getAllStackTraces执行额外的方法,如下所示Java 访问自定义线程方法,java,multithreading,Java,Multithreading,我用额外的方法创建了一个自定义线程。我在循环中创建了几个线程 我想知道是否可以使用Thread.getAllStackTraces执行额外的方法,如下所示 public class CustomThread extends Thread { int pid; CustomThread(int processID) { this.pid = processID; } @Override public void run()
public class CustomThread extends Thread
{
int pid;
CustomThread(int processID)
{
this.pid = processID;
}
@Override
public void run()
{
System.out.println("Thread running");
}
public void printDetails()
{
System.out.println("PID "+this.pid);
}
}
public class Main
{
public static void main(String[] args)
{
for(int i = 0;i<5;i++){
CustomThread ct = new CustomThread(1);
ct.start();
}
}
System.out.println(Thread.getAllStackTraces().get(0).printDetails); <- Is it possible to access the method like this?
}
getAllStackTraces返回所有活动线程的堆栈跟踪映射。映射键是线程,每个映射值是一个StackTraceElement数组,表示对应线程的堆栈转储
由于返回了Map,所以无法使用get0获取元素。您必须提供线程实例作为密钥,但您将获得StackTraceElement[]。
在本例中,您需要Thread.getAllStackTraces.keySet.get0来获取第一个线程,或者您可以迭代整个映射键
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t instanceof CustomThread) {
((CustomThread)t).printDetails();;
}
}
getAllStackTraces返回所有活动线程的堆栈跟踪映射。映射键是线程,每个映射值是一个StackTraceElement数组,表示对应线程的堆栈转储
由于返回了Map,所以无法使用get0获取元素。您必须提供线程实例作为密钥,但您将获得StackTraceElement[]。
在本例中,您需要Thread.getAllStackTraces.keySet.get0来获取第一个线程,或者您可以迭代整个映射键
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t instanceof CustomThread) {
((CustomThread)t).printDetails();;
}
}
在打字的时候,我看到Evgeny在打字速度上打败了我,不过我想指出一些事情。他的方法是正确的,但不会给您任何输出: 首先是对方法的详细说明:
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
Set<Thread> threads = map.keySet();//Get the keys of the map, in this case the key is the thread
for(Thread thread : threads){//iterate over all the threads
if(thread instanceof CustomThread){//check to see if it is one of our custom threads
CustomThread custom = (CustomThread)thread;//cast it to a custom thread
custom.printDetails();//call your method
}
}
因此,为了测试它,您可以向线程添加一个无限while循环,然后该方法就可以工作了
此外,所有线程都将具有与相同的PID,而不是使用i作为循环中使用1的PID。因此,要为所有自定义线程提供不同的PID,您可以使用:
for(int i = 0;i<5;i++){
CustomThread ct = new CustomThread(i);
ct.start();
}
我希望这有助于:
p.S.Evgeny是第一个获得荣誉的人,所以应该获得荣誉。在打字时,我看到Evgeny在打字速度上击败了我,不过我想指出一些事情。他的方法是正确的,但不会给您任何输出: 首先是对方法的详细说明:
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
Set<Thread> threads = map.keySet();//Get the keys of the map, in this case the key is the thread
for(Thread thread : threads){//iterate over all the threads
if(thread instanceof CustomThread){//check to see if it is one of our custom threads
CustomThread custom = (CustomThread)thread;//cast it to a custom thread
custom.printDetails();//call your method
}
}
因此,为了测试它,您可以向线程添加一个无限while循环,然后该方法就可以工作了
此外,所有线程都将具有与相同的PID,而不是使用i作为循环中使用1的PID。因此,要为所有自定义线程提供不同的PID,您可以使用:
for(int i = 0;i<5;i++){
CustomThread ct = new CustomThread(i);
ct.start();
}
我希望这有助于:
p.S.Evgeny是第一个获得信用的人。printDetails方法属于CustomThread,但是Thread.getAllStackTraces.get0返回StackTraceElement[]arrayprintDetails方法属于CustomThread,但是Thread.getAllStackTraces.get0返回StackTraceElement[]arrayHi。感谢您的回复。我使用了这个答案,但是,尽管创建了5个线程,循环还是会迭代一次。如果我删除instanceof条件语句,它将迭代所有5个线程。@Vimlan.G循环迭代一次,因为其他线程在调用getAllStackTraces之前完成了工作。罗恩在回答中提到了这一点。您需要向CustomThread运行方法添加一些内容,以使线程工作更长时间,例如Thread.sleep或无限循环。如果删除实例,您可能会得到ClassCastException.Hi。感谢您的回复。我使用了这个答案,但是,尽管创建了5个线程,循环还是会迭代一次。如果我删除instanceof条件语句,它将迭代所有5个线程。@Vimlan.G循环迭代一次,因为其他线程在调用getAllStackTraces之前完成了工作。罗恩在回答中提到了这一点。您需要向CustomThread运行方法添加一些内容,以使线程工作更长时间,例如Thread.sleep或无限循环。如果删除了的实例,可能会得到ClassCastException。