-2

I know some basic concepts in java multithreading.But now i want to create 5 threads that should work concurrently.How can i get execution time of a thread?...Somebody please help me with the deep concepts of threads including methods and purposes.

0

4 Answers 4

2

Your question is really unclear. What do you mean by execution time of a thread? When it started vs. when it stopped (wall time) Or how long it was actually running, not including times it was on hold (i.e., CPU time)?

Take a look at Monitor cpu usage per thread in java?

BTW, Threading isn't something you can simply learn from a StackOverflow answer.

The official guide to Java explains concurrency quite well: http://docs.oracle.com/javase/tutorial/essential/concurrency/

The book "Java Concurrency in Practice" is even better.

Sign up to request clarification or add additional context in comments.

1 Comment

pardon my ignorance. I still don't get how we can get the actual execution time of a thread?.
1

Make a proxy

class Proxy implements Runnable {
    final Runnable target;

    Proxy(Runnable target) {
        this.target = target;
    }

    public void run() {
        long t0 = System.currentTimeMillis();
        try {
            target.run();
        } finally {
            System.out.println(Thread.currentThread() + " execution time = " + (System.currentTimeMillis() - t0));
        }
    }
}

and use it

new Thread(new Proxy(task)).start();

Comments

0

You can use the methods of

ThreadMxBean interface

you can get the instance using

 ManagementFactory.getThreadMXBean();

after that you can call a method

getThreadCpuTime(Thread.currentThread().getId());

so your code will look like

ManagementFactory.getThreadMXBean.getThreadCpuTime(Thread.currentThread().getId());

for more details see Docs

Comments

0

Something like this code could be useful http://blog.sheidaei.com/2013/06/simple-thread-example-in-java.html.

You can use System.currentTimeMillis() instead of System.out.println() to get the execution time of the threads.

/**
 * Created with IntelliJ IDEA.
 * User: shahin
 * Date: 6/5/13
 * Time: 11:32 PM
 * To change this template use File | Settings | File Templates.
 */
public class SimpleThread implements Runnable{

    public SimpleThread(String simpleName) {
        this.simpleName = simpleName;
        System.out.println(">>> Constructor for " + getSimpleName());
    }

    public String getSimpleName() {
        return simpleName;
    }

    public void setSimpleName(String simpleName) {
        this.simpleName = simpleName;
    }

    private String simpleName;
    @Override
    public void run() {
        System.out.println(" >> "+getSimpleName() + " started.");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        System.out.println(" >> "+getSimpleName() + " stopped.");
    }

    public static void main(String args[])
    {
        System.out.println("Main Thread started.");

        SimpleWaitNotifyThread simpleThread;
        Thread thread;
        for(int i=0;i<5;i++)
        {
            simpleThread = new SimpleWaitNotifyThread("Thread "+(i+1));
            thread = new Thread(simpleThread,"Thread "+(i+1));
            thread.start();
        }

        System.out.println("Main Thread finished.");
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.