CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy 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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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.

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy

    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) { ... }
    }}

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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.

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy

    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...

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Talking

    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
  •  





Click Here to Expand Forum to Full Width

Featured