I want to find which java thread is modifying file.
If anybody know tool or know how we can monitor and get thread name please share.
Thanks.
I am not sure why exactly you need this information but i beleive there are two ways.
First way is in your java code when you create a thread , create it with a name. There are constructors & method available in Thread class to do the same. If you have a good logging in your project then you can log the thread name who enters the code to modify the file.
Second way is again dependent on having a thread with the name. You can use a java profiler to find out what threads are doing at a point in time. Although it will be too tough if threads are modifying the file too quickly. But if you are trying to find a problem of slow modification of file by a thread then profiler may give you the hint.
Hope it helps!
if the file modification is a code that you wrote, you can use before the modification:
Thread.currentThread().getName();
Thread.currentThread().getId();
and youl get the name and id of the thread
Related
I have two Java processes and need to make sure that they do not simultaneously access directory /dir. I am not sure how to properly implement this behaviour.
My idea would be to define a certain file lock.txt and do something like
if not (lock.txt exists)
{
create lock.txt with content "process 1"
do something in /dir
delete lock.txt
}
But I guess I could run into some kind of race condition if both processes check this simultaneously.
EDIT: my Java processes are separate programs.
Look at the FileLock class here: http://docs.oracle.com/javase/6/docs/api/java/nio/channels/FileLock.html
You could have found this with a bit of googling
So I have some code that I'm modifying. Without going into huge details about what it is actually doing, it can be summarized by creating a file and then sending it to a printer. Essentially the following:
File file = new File("/tmp/12345.pdf");
//Lots of magical code that creates/writes to said file
...
...
...
//sendToPrinter essentially builds up a print command to send to /usr/bin/lp and then executes it.
sendToPrinter(printer, details, file);
file.delete();// This is the line I'm curious about.
My question is that the call to lp is made before we get to file.delete(). However, does this run the risk of creating a race condition where I actually end up deleting the file before the printer is ready?
From preliminary testing, I have yet to see a problem, but something about this bothers me. Have I created the risk of a race condition by doing this?
Edit: Clarification based upon comments. Yes, I'm utilizing lp and sendToPrinter does appear to be waiting for a return code. The code is not asynchronous.
From doc
The join method allows one thread to wait for the completion of
another. If t is a Thread object whose thread is currently executing,
t.join(); causes the current thread to pause execution until t's
thread terminates.
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
I'm using AspectJ to monitor my application performance. E.g. start time, end time, memory consumption, etc.
I have a threadpool in my main package with 4 fixed threads executing a particular function. I need to check the thread ids of these threads when the particular function executes. I have a pointcut on this method, but I'm not sure how to get the thread id.
I know that I can use the after returning advice and the get the returned object in my advice. Is there a way to get all the objects created in a method. I'm assuming I'd need an after advice. But I'm not sure how to proceed further.
How about
Thread.currentThread().getId()
There is really nothing special in AspectJ with regard to threading. Aspect code is executed in the same thread it was woven into.
If I do not understand your question correctly and the above does not answer it, please just update your question and provide an SSCCE in order to make the audience see and understand what the point is.
If an application starts many threads ( using new Thread() as well as using ExecutorService ) at various places in its code ( including from within the referenced jars ) , then what is the best way to identify the source code that started any specific thread ( as seen in an executing instance of the application )
This is helpful , for example , in case a thread causes an Exception - and we need to start at the source code where the thread was initialized ( so that the context is clear ).The thread dump I get from VisualVM shows many running/waiting threads but the stack root always seem to be at java.lang.Thread.run(Thread.java:722) - not very helpful.
`
Are you able to insert code at the points that the threads are being created? If so, then create a static HashMap in your main class along with a public static put method. Then whenever you create a thread t1, call Main.putThreadId(t1.getId(), "some text that identifies the method that's creating the thread"), and when you catch an exception look up the value in the HashMap.
What you can do is that, set Thread.uncaughtExceptionhandler to every thread you start.
It has a method called:
uncaughtException(Thread t,Throwable e)
Inside this you can put a log or something with which you can later identify from where this code is called for. But of-course for every thread, you will have to specify individually as the origin.
You can set a global one by Thread.setDefaultUncaughtExceptionHandler(myHandler);. But to make it distinct, probably have a ThreadGroup or something.
Though all this isn't helpful if the thread is started by referenced Jar.
You can use the map approach given before, but instead of some text, you generate a new Exception at the point where you create the thread. Put this exception as value in the map. You can later get the stack trace if you need. For Executors, you can hide this in the ThreadFactory creating the worker threads.
This problem occurs in similar fashion when using Runnables. Sometimes you want to know where the Runnable has been created and queued, on top of any stack trace starting at the run method.
Of course the whole legacy code and referenced jar problem is: you want to solve a problem that needs coding, but without coding. Not easy :)
Just as the title. Google didn't give me any clue.
Edit: What I mean is to get the threads that executing the code of the servlet. Thanks.
Edit: Why I want this information, this is because when there are many threads executing, their log aggregate in a single log, and the order of log is disrupt. I want a thread id inserted at the front of each line of the log so that I can trace the activity of each thread.
It is not actually a meaningful thing to ask for.
Threads don't belong to a servlet. Rather, they belong to the web container and are used to run requests ... which at certain points involves running servlet methods. A servlet method can of course find out what the current thread is ... but then so can any method.
It is also possible that the web container might use thread groups in a way that allows you to determine that certain threads are used for certain things. But that would be highly implementation specific.
If that's not what you mean, then please refine your question.
What I mean is to get the threads that executing the code of the servlet.
Do you mean currently executing the code of the servlet?
Then I think that the answer is simply - "This is not possible".
It is not possible within a running program for one application thread to find out what code another thread is executing. This kind of thing can only be found using a debug agent ... while all application threads are stopped.
I want a thread id inserted at the front of each line of the log so that I can trace the activity of each thread.
(Well why didn't you simply ask that in the first place??)
A log4j LoggingEvent contains the name of the thread that created the event. You can use a %t in a pattern layout to include the thread name in a log file. You could also write your own custom Appender to filter the events into different "streams" based on the event's thread name.
The thread id is not available for logging ... unless you explicitly insert it into (for example) the log message string.
How about:
long threadId = Thread.currentThread().getId();
in the servlet code? That will give you the ID of the thread running the servlet's service() method. Obviously many of those can be going on simultaneously. Or do you mean something else by "id of the threads of the servlet"?
You can get all the threads in the current thread group with this code:
int active = Thread.activeCount();
Thread allThreads[] = new Thread[active];
active = Thread.enumerate(allThreads);
You can then call getId() for each element of the returned array. You can get fancier by first climbing to the root ThreadGroup of the current thread.
What would you want this information for?
You can get only the currrent thread id using Thread.currentThread().getId(). If you want to track all the threads executing your servlet, keep a list in the request called executedThreads and add the current thread id to the list.