• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I cannot lock myPrint() even I put synchronized. How can I do that? please help.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have 3 distinct instances of TRead. Each one has its own lock, and that's what each one is syncing on, so there's no mutual exclusion among them. You would need to sync all on the same lock.
 
Cham Wick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. But can you please write a piece of code on how to do that.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cham Wick wrote:Thanks. But can you please write a piece of code on how to do that.



Yes, I can.

I won't though. I told you was has to be done. Now it's up to you so ShowSomeEffort and try to do it. Employ some logic and educated guessing, try a couple things, and if you get stuck, show what you tried and explain what difficulty you're still having.

Good luck!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code has some strange things.

Why do you have a static member variable TRead tr in line 6? You have a method getInst() (lines 21-23) which I would have expected to be static too (since all it does is return the static member variable). That method is not called anywhere (well, it is in line 27, but that's commented out).

Did you understand what Jeff said? Synchronization in Java happens on objects. Each Java object has a lock associated with it. When you synchronize on an object, the thread that wants to execute the synchronized block or method will try to acquire the lock for the object. Only one thread can hold the lock of an object, so if another thread already holds the lock, the thread that tries to execute the block is forced to wait until the other thread releases the lock.

In your code as it is now, you are starting three threads, but you have three different TRead objects. The three threads will lock those three different objects, which they can do without needing to wait; only when threads try to lock the same object, there's a chance they have to wait for each other.

You'll have to make some fundamental changes to your code to see it work. One thing is that you should not make TRead extend Thread, or make the code synchronize on something else than a TRead object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic