0

I am using HttpsURLConnection in Java and through some sort of loop i am creating 10 connections which later on used by different threads inside my program.

HttpsURLConnection connection = null;
connection =    (HttpsURLConnection) url.openConnection();

Wondering how can i know which connection is used by current thread. I Just need some unique identifier of the connection.

Some thing like,

System.out.println(connection);

But above System.out statement is printing pretty generic server name. Not the hash code of connection object.

Wondering how can i do that?

Ammad
  • 4,031
  • 12
  • 39
  • 62
  • 2
    Maybe `connection.hashCode()` ? – TeWu Apr 06 '17 at 22:43
  • Thanks Tewu. This Api method was some how over looked. Please reply in answer box. – Ammad Apr 06 '17 at 22:53
  • Or [`System.identityHashCode(connection)`](http://stackoverflow.com/questions/4930781/how-do-hashcode-and-identityhashcode-work-at-the-back-end) – zapl Apr 06 '17 at 23:14

2 Answers2

2

The hashCode method is defined on java.lang.Object (and overridden in subclasses when needed), so you can call it on any object. In your case connection.hashCode() should give you the hash code of connection object.

TeWu
  • 5,928
  • 2
  • 22
  • 36
0

Hashcode could be same for both. Remember Hashcode and equals contract in javadocs.

please try to avoid relying on hashcode. As its not a good practice and in general cases hashcode are different each time. But its not assured to be unique.

You can use something like connection management api.

Please refer to below link. May be it can help.

If you can share your code or whole picture i can help in better way.

http://www.baeldung.com/httpclient-connection-management