[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: AsyncIOExample.SampleAsyncFunction

Correct, either counter must be made static, or both the executorService and random non-static (which makes the counter unnecessary).

For an example I would regard the latter as preferable to ensure that instances don't interfere with each other.

As for your question, for a given instance open() is only called once.

On 06.07.2018 15:33, NEKRASSOV, ALEXEI wrote:

I'm confused by lines 119-122 in

Why is counter non-static, but executorService and random - static?

It's not clear to me whether open() can be called more than once on a given object, and thus it's not clear whether the intent is to count the number of open's for every object (the current code). Or we really need to make counter static - to count the number of objects that share executorService and random?..

Having executorService and random as static, but counter as instance-specific will not work when someone creates a second instance of SampleAsyncFunction. In second SampleAsyncFunction the counter will be 0 and we will re-intialize static executorService and random, thus interfering with the first SampleAsyncFunction object.