|
-
January 28th, 2003, 03:04 AM
#1
Question on "synchronized" keyword?
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
-
January 28th, 2003, 08:52 AM
#2
Try Synchronizing Threads and for the real meat, you can't beat the language spec:Threads and Locks
Things do not chage; we change
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 28th, 2003, 09:12 AM
#3
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) { ... }
}}
-
January 28th, 2003, 12:14 PM
#4
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 29th, 2003, 12:00 AM
#5
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, 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...
-
January 29th, 2003, 04:06 AM
#6
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|