Click to See Complete Forum and Search --> : Question on "synchronized" keyword?


George2
January 28th, 2003, 02:04 AM
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

dlorde
January 28th, 2003, 07:52 AM
Try Synchronizing Threads (http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html) and for the real meat, you can't beat the language spec:Threads and Locks (http://java.sun.com/docs/books/jls/second_edition/html/memory.doc.html#28270)

Things do not chage; we change

George2
January 28th, 2003, 08:12 AM
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) { ... }
}}

dlorde
January 28th, 2003, 11:14 AM
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 (http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#255769), and 8.4.3.6 synchronized Methods (http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#260369) 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...

George2
January 28th, 2003, 11:00 PM
Thanks, dlorde buddies!

BTW: You looks like my boss from the photo. :-)

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 (http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#255769), and 8.4.3.6 synchronized Methods (http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#260369) 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...

George2
January 29th, 2003, 03:06 AM
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 (http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#255769), and 8.4.3.6 synchronized Methods (http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#260369) 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...