EmperorPenguin
EmperorPenguin

Reputation: 83

asynchronous function calls in Java

Suppose you have three classes A,B,C each having its own search function. I want to run a key ( say 'searchKey' ) concurrently using all the three search functions. How do I stop the other two search functions if I get the result from one of the three functions ?

Also would this run faster than the case where I have a distinct hashmap in each class and search them one after the other since the search resolves to a constant time complexity ?

Upvotes: 5

Views: 102

Answers (1)

Murat Karagöz
Murat Karagöz

Reputation: 37594

You would need to expose a public method to stop the search e.g. a flag to cancel the Threads in their respective class.

To your second point, the time complexity for a key search in a HashMap is usually O(1) (worst case O(n) if those keys are in the same hash bucket). So there is not much room for optimization since it's already blazing fast. You would not even notice that if you are searching the HashMaps in sequence.

Upvotes: 3

Related Questions