S
S
synapse_people2016-01-19 13:57:13
Java
synapse_people, 2016-01-19 13:57:13

Synchronization in JAVA?

In general essence: There is an entry method that sends an entry packet to the server and lach (CountDownLatch) with a count of 1, which decreases when a response comes from the server - the result of the entry.
The input is something like this:
1. create a lach
2. send a packet
3. wait for lach (await)
4. We get account data, which are stored simply in the class with the "data" field.
The call from the server is something like this:
Check the answer, if necessary, add the data to data and lower the latch.
How can I make it perfect, because sometimes due to the fact that the call from the server in another thread does not have time to set the field in the class and a zero pointer falls out in the login method ...
In general, what is the best way to synchronize the input method if the result comes asynchronously, and the input method must be synchronous and blocking ..
At the same time, if the method was called, but the result did not come and the methods were called again, you need to throw an exception ..
Advise. Thank you!
AP: There too..
What if you have a method that returns boolean - canHandle, if it is true, then the login method can be called, if it is false, then there is no connection and the login method will throw an error.
How can you wait in Java for a method to return. true? while(canHandle()) don't suggest plz...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
razer89, 2016-01-19
@synapse_people

You should look towards Listeners (event listeners)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question