Hi, everyone!
I find I mis-understand some basics of the keyword.
Where can I find some simple samples on how to use
"synchronized" keyword?
Thanks in advance,
George
Printable View
Hi, everyone!
I find I mis-understand some basics of the keyword.
Where can I find some simple samples on how to use
"synchronized" keyword?
Thanks in advance,
George
Try Synchronizing Threads and for the real meat, you can't beat the language spec:Threads and Locks
Things do not chage; we change
Thanks, dlorde buddies!
Please see the following example, I do not understand
how "synchronized" method can make thread safely. In such
example, why "lock is associated with the object" ?
Can you show me a case with the sample that how the lock is
associated with the object?
Thanks in advance,
George
public class Database {
private Object studentLock = new Object();
private Object companyLock = new Object();
public Student createStudent()
{
synchronized(studentLock) { ... }
}
public Company createCompany()
{
synchronized(companyLock) { ... }
}}
The lock is associated with the object by the synchronized statement itself. You'll find full and detailed explanations of synchronized and its usage in the two links I posted previously. Make the effort, read the docs.
In particular, 14.18 The synchronized Statement, and 8.4.3.6 synchronized Methods tell the full story of synchronized statements and methods and their equivalence. You will see (if you can raise the effort to look) that you normally don't need to explicitly declare lock objects like studentLock and companyLock.
Sometimes you just have to do things for yourself...
Thanks, dlorde buddies!
BTW: You looks like my boss from the photo. :-)
Quote:
Originally posted by dlorde
The lock is associated with the object by the synchronized statement itself. You'll find full and detailed explanations of synchronized and its usage in the two links I posted previously. Make the effort, read the docs.
In particular, 14.18 The synchronized Statement, and 8.4.3.6 synchronized Methods tell the full story of synchronized statements and methods and their equivalence. You will see (if you can raise the effort to look) that you normally don't need to explicitly declare lock objects like studentLock and companyLock.
Sometimes you just have to do things for yourself...
Quote:
Originally posted by dlorde
The lock is associated with the object by the synchronized statement itself. You'll find full and detailed explanations of synchronized and its usage in the two links I posted previously. Make the effort, read the docs.
In particular, 14.18 The synchronized Statement, and 8.4.3.6 synchronized Methods tell the full story of synchronized statements and methods and their equivalence. You will see (if you can raise the effort to look) that you normally don't need to explicitly declare lock objects like studentLock and companyLock.
Sometimes you just have to do things for yourself...