can you please explain to me this piece of java code? I cannot understand this syntax.
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It means that this block of code is
synchronizedmeaning no more than one thread will be able to access the code inside that block.Also
thismeans you can synchronize on the current instance (obtain lock on the current instance).This is what I found in Kathy Sierra’s java certification book.
Because synchronization does hurt concurrency, you don’t want to synchronize
any more code than is necessary to protect your data. So if the scope of a method is
more than needed, you can reduce the scope of the synchronized part to something
less than a full method—to just a block.
Look at the following code snippet:
which can be changed to this:
In the second snippet, the synchronization lock is only applied for that block of code instead of the entire method.