3

I am running the main method of a class twice, simultaneously via different executions and was looking at what would uniquely identify each one as it was running. It was simple to create something in myself:

String sid = UUID.randomUUID().toString(); 

...but is there anything inherent in each running execution - part of the executing class - that I could use as a unique identifier.

We did a few approaches ourselves to see that we didn't know of something available we could use. We first wondered about using Thread.currentThread().getName() but both are main. I can't use toString() because there is no instance.


Edit:
This class (GettingRollingClient) is what I'm running twice simultaneously, trying to differentiate between the two as they run... re-using a Java feature if I can:
java -cp "hazelcast-client-3.0.1.jar:hazelcast-3.0.1.jar:." GettingRollingClient
Crowie
  • 3,220
  • 7
  • 28
  • 48
  • _I am running the main method of a class twice_ A method doesn't have an identifier. Are you running it in the same thread? – Sotirios Delimanolis Sep 06 '13 at 14:30
  • Sorry didn't know how to say - "running the same class twice simulataneously". Hope the edit helps – Crowie Sep 06 '13 at 14:33
  • 1
    Try `Thread.currentThread().getId()` or `Thread.currentThread().getName()` to see if they work for you, or look further into the Thread class http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html – Marcelo Sep 06 '13 at 14:37
  • Each running instance has only one thread, so the thread id is 1 for each instance, and thread name is main for both – Crowie Sep 06 '13 at 14:44
  • Ok @Crowie, this is what you want then: http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id – Marcelo Sep 06 '13 at 14:52
  • 1
    @Marcelo I really don't like that approach due to the platform dependency it creates. – dbyrne Sep 06 '13 at 14:54
  • @Marcelo Cheers mate I had a read. So there is a way but I agree with dbyrne. It seems to me that there is nothing simple in place because there isn't a simple, single, cut-and-dried answer to getting a PID. – Crowie Sep 06 '13 at 15:02

1 Answers1

2

Your current approach of generating a unique id yourself is the best idea.

The Java and JVM specifications do not define a natural unique identifier. This means that any inherently pre-existing id will be specific to either the operating system or the JVM implementation. This will tie your code to a specific platform and make it less portable.

Whether or not UUID.randomUUID() is the best way to generate an id yourself depends on your usecase. One alternative would be to use a shared temp file with a counter to keep track of all the currently running processes.

dbyrne
  • 59,111
  • 13
  • 86
  • 103
  • 1
    In theory, since there are 2 different JVM processes, `UUID.randomUUID()` could return the same value for both runs. – Marcelo Sep 06 '13 at 14:58
  • @Marcelo There are easy ways to avoid that (highly unlikely) scenario such as using a temp file to store all the currently active process ids. – dbyrne Sep 06 '13 at 15:01
  • You are right, I think using a file would be the correct solution for the poster's problem, not really UUID generation and not really capturing JVMs process ID. – Marcelo Sep 06 '13 at 15:05
  • Wanted a solution that already exists because I suspected there was something I was missing. Looks to me, IMHO, like there is nothing because generating one is a better idea.... – Crowie Sep 06 '13 at 15:05