Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to print name of the thread that enters a synchronized method

635127Apr 29 2008 — edited Apr 29 2008
Hello,

I've a simple java program with multiple threads running. I also have the below synchronized method in my program.

public synchronized static int getReadIndex()
{
int currentIndex = readIndex;
readIndex = readIndex + chunkSize; //update readIndex to point to next chunk
return(currentIndex);
}

I want to generate some statistics and want to print the thread name and the corresponding index value whenever it enters the synchronized method.

Can anyone please tell me how to do that.

This is my thread class

private static class DatabaseThread extends Thread
{
public void run(){.....}
}

and this is how I create a thread object:

private static DatabaseThread myThread1 = new DatabaseThread();

Also, how do I specify thread name while creating the thread.

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 27 2008
Added on Apr 29 2008
8 comments
381 views