1

I have a class that implements Runnable interface. When I start the the thread I give it a name so I could find the thread later on

LoadRawDataThread loadRawDataThread = new LoadRawDataThread(filePath + fileName, delimiter, noOfColumn, sqlDBname, 
                        FileUtil.getParameter("transactionType_" + hiddenFieldNo, parameterMap),
                        tableName, seqNo, requestBulkSize, encoding);
                Thread thread = new Thread(loadRawDataThread);
                thread.setName(fileName);
                thread.start();

Now, in other method I would like to get this loadRawDataThread object because I have volatile there that I would like to grab. I did something like this but this will return Thread object hence I cant see my volatile there.

private Thread getThreadByName(String threadName) {
    for (Thread t : Thread.getAllStackTraces().keySet()) {
        logger.info("The name of the thread is :------->>>>>>> " + t.getName());
        if (t.getName().equalsIgnoreCase(threadName.trim())) {
            return t;
        }
    }
    return null;
}

Any ideas how could it be done?

mat_zat
  • 99
  • 1
  • 9
  • 2
    I think you're solving the wrong problem. Move your variable or the task somewhere where it's easier to find later. For example, have a collection (or dictionary) of running tasks that you can access. – M. Prokhorov Jul 13 '17 at 15:23
  • 1
    This question has nothing to do with threading. Your program explicitly creates an object. You want to associate the object with a name, and you want to look up the object by name later on. How about using a `Map`? – Solomon Slow Jul 13 '17 at 15:38
  • @jameslarge - thanks, that solved my problem, so simple :) – mat_zat Jul 14 '17 at 06:45

0 Answers0