YouDontKnowAboutBear
YouDontKnowAboutBear

Reputation: 28

Is is possible to get the reference of a running thread?

It was my first post on the community. Any comments and suggestions are welcome. I have a web service on end point http://ip:port/report. This service execute a Runnable class. After executing the Runnable, is it possible to check / monitor the thread with a separate service call?

I am still working and searching for some stuff like ExecutorService, but still no luck. Is there any other way to do this? Please suggest some probable solution that I can check. Sorry for my grammar. I hope I can explain it clear with my sample code below.

My code is something like this.

Running the thread on : http://ip:port/report

public String runReport {

    // Run the the runnable
    Report report = new Report();
    String threadName = "REPORT1";
    Thread t = new Thread(report, threadName);
    t.start();

    // return thread some details
    return t.getId() + "|" + t.hashCode();
}

My runnable class

public class Report {

    private String status;

    @Override
    public void run() {
        //Update status
        setStatus("Running");

        //... do stuff

        //Update status
        setStatus("End")
    }

    // Getter and Setter
}

My checker class on http://ip:port/report/check/some_param

public String check( int threadId ) {
    // Search the thread by threadId and check the current status
    //Report.getStatus();
}

Upvotes: 0

Views: 791

Answers (2)

Somil Aseeja
Somil Aseeja

Reputation: 170

Maintain the map in the class of your job id. Whenever that thread is initialized, pass the id in the constructor and when it starts processing put the status as running and when it gets completed, just before ending execution in run method, put the status as end. Something like below

public void run(){
     try{
     jobsMap.put(this.jobId, "Running");
     // your business logic here
     jobsMap.put(this.jobId,"Completed");
     } catch(Exception e){
       jobsMap.put(this.jobId,"Failed. Reason -"+e.getMessage);
       // exception handling
     }
}

Upvotes: 1

ernest_k
ernest_k

Reputation: 45319

Using thread IDs may not be the best idea, especially because you're likely to use a pool that reuses threads. A simple solution is to generate IDs for your jobs and maintain a map that you can use to read the status.

As an example, you can use unique IDs for your task IDs:

Map<String, Report> jobs = new ConcurrentHashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(10); //just an example

public String runReport {

    // Run the the runnable
    Report report = new Report();

    //For a numeric sequence, you can use something like AtomicLong
    String jobId = UUID.randomUUID().toString();
    jobs.put(jobId, report);

    //using a thread pool may be a better idea.
    executorService.submit(report);

    // return the job ID
    return jobId;
}

And to check the status, you just read the map:

public String check(String jobId) {
    return jobs.get(jobId).getStatus(); //remember null checks
}

You would then just need to know when to remove entries from the map based on how you expect the check method to be called.

Upvotes: 1

Related Questions