Get List of Threads

Get a list of all threads currently running in Java

To get an iterable set:

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

Performance: 0 ms for 12 threads (Azul JVM 16.0.1, Windows 10, Ryzen 5600X).

On Linux, in C, how can I get all threads of a process?

The code I am using, based on reading /proc

#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>

Then, from inside a funcion:

    DIR *proc_dir;
{
char dirname[100];
snprintf(dirname, sizeof dirname, "/proc/%d/task", getpid());
proc_dir = opendir(dirname);
}

if (proc_dir)
{
/* /proc available, iterate through tasks... */
struct dirent *entry;
while ((entry = readdir(proc_dir)) != NULL)
{
if(entry->d_name[0] == '.')
continue;

int tid = atoi(entry->d_name);

/* ... (do stuff with tid) ... */
}

closedir(proc_dir);
}
else
{
/* /proc not available, act accordingly */
}

Get list of threads

using System.Diagnostics;

ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;

foreach (ProcessThread thread in currentThreads)
{
// Do whatever you need
}

How do you get list of running threads in C#?

Create a List<Thread> and store each new thread in your first for loop in it.

List<string>[] list;
List<Thread> threads = new List<Thread>();
list = dbConnect.Select();

for (int i = 0; i < list[0].Count; i++)
{
Thread th = new Thread(() =>{
sendMessage(list[0]['1']);
//calling callback function
});
th.Name = "SID"+i;
th.Start();
threads.add(th)
}

for (int i = 0; i < list[0].Count; i++)
{
threads[i].DoStuff()
}

However if you don't need i you can make the second loop a foreach instead of a for


As a side note, if your sendMessage function does not take very long to execute you should somthing lighter weight then a full Thread, use a ThreadPool.QueueUserWorkItem or if it is available to you, a Task

How to list threads in the same process?

You can do this with WMI (Windows MIcrosoft Instrumentation).
Here there are an article that explain how to retrieve all threads running of one process. The article is in spanish, but you can use authomatic translation o view the code and download samples.

Using WMI you can obtain all information of a process using the class Win32_Process class.
You can try execute in a console a coomand like this, to obtain information of this class.

WMIC Process where name="bds.exe" GET Name, description, ProcessId,
ThreadCount, Handle

Sample Image

With this you can obtain information of process.

The second step if "How to retrieve the Threads associated a process". You can do this with the Win32_Thread class.

If you launch a query like this:

SELECT * FROM WIN32_THREAD WHERE ProcessHandle=10740

You get all the threads of the process 10740 (see ProcessId of the first query).

Regards.

How to get Threads of a Process and show it in a list

The main issue is because the type of thread is Process, but you are returning a collection of ProcessThread's (Dim myThreads As ProcessThreadCollection = CurrentProcess.Threads), they are not the same.

To fix you have a few options:

  1. Change the type of thread to ProcessThread. For ex: Dim thread As ProcessThread (this will fix the actual error).

  2. Remove the variable thread altogether and change your loop; you should do this. For ex:

    For Each thread As ThreadProcess In myThreads

Python getting running Threads

You can use threading.enumerate() : Python documentation about it here

for thread in threading.enumerate(): 
print(thread.name)


Related Topics



Leave a reply



Submit