• 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

locking on an object WITHOUT synchronized keyword : my LockSimulator fails !

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to ensure that only one thread can access the method(s) of an object at a time without using the keyword synchronized. I am a newbie to CS/IT , so i dont know if this will be easy to implement. I know small bits of information about computers and just java (Thats all !) . Please tell me if i need more knowledge to make such a program. If it can be implemented easily, please give me hints and suggestions. Am i doing something foolish ? Please let me know.

A small description :

ThreadSafe class - any class that extends this will be able to check if some thread has acquired a lock on it and let its instance be accessed by one thread at a time.
The checkOwner() is used to control the access to the methods of subclasses.
The release() is invoked by a thread to show that it has released the lock on an object.

Room extends ThreadSafe - this has a only a method to show which "Sleeper" thread is accessing this Room.

Sleeper extends Thread - this has reference to a Room object and simply runs the useRoom() method of a Room instance.

LockSimulator class - has the main() method. It creates 5 Sleepers with unique names and a reference/key to the same Room object. And as per my goal, only one Sleeper can use a Room at a time. Others have to wait till the current sleeper releases the room.

Here is the working code of my failed simulator :



You can run this and get unpredictable output...here is a sample :

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this the appropriate sub-forum for such a post ?
 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this forum is just fine.

Rahul Sudip Bose wrote:I am trying to ensure that only one thread can access the method(s) of an object at a time without using the keyword synchronized.



This is impossible in Java, unless you use busy waits, or sleep loops, which are very bad ideas. It simply can't be done. In order to stop threads, you have to use Object.wait() and Object.notifyAll(), and these require ownership of the object monitor, through the synchronized keyword.

I guess one way would be to write native code that uses mutex, and call it through JNI. But in pure Java it can't be done.

Can I ask why you want to do this?
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Can I ask why you want to do this?



I cooked up a problem for myself to solve, so that i could get some practice. JNI etc... i dont know those things...maybe i should abandon this.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A more useful challenge might be to practice using "synchronized" in various
multi-threaded situations to see how your data is, or is not, properly protected.
Do you have a solid understanding of when and how to use wait() and notify()?

Jim ...
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:
Do you have a solid understanding of when and how to use wait() and notify()?


I am a beginner and still learning. Only a test and real-coding can answer the above question.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic