Does ruby have the Java equivalent of synchronize keyword?
It doesn't have the synchronize
keyword, but you can get something very similar via the Monitor
class. Here's an example from the Programming Ruby 1.8 book:
require 'monitor'
class Counter < Monitor
attr_reader :count
def initialize
@count = 0
super
end
def tick
synchronize do
@count += 1
end
end
end
c = Counter.new
t1 = Thread.new { 100_000.times { c.tick } }
t2 = Thread.new { 100_000.times { c.tick } }
t1.join; t2.join
c.count → 200000
Is synchronized deep in java?
Asking whether synchronization is "deep" shows that you don't yet fully understand how Java thread "synchronization" works. All beginning Java programmers usually have the same misconception -- "synchronizing on f
makes access to f
's fields thread-safe". This is not true.
When one thread is executing your synchronized(f)
block, no other thread can enter any block or method synchronized
on the same object. THAT IS ALL.
There is no reason why you must protect the mutable fields of f
by synchronizing on f
. You can synchronize on any Object at all you want, as long as you always use the same one, and as long as you always synchronize when you are accessing/modifying the fields of f
.
Conversely, synchronizing on f
does NOT mean that your access of f
's fields is necessarily thread-safe. You could have some other code elsewhere in the program, modifying f
's fields without any synchronization at all, or synchronizing on a different Object.
If this seems confusing, that's because it is. The designers of Java made a bad choice (to associate an implicit mutex with each and every object), and we just have to deal with it.
Synchronized (Hash)Map of Singletons
According to the docs for synchronizedMap()
Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.
It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views
In other words you still need to have synchronized
access to SHARED_ACCOUNT_HASHMAP
:
public init(String[] credentials) {
Account account = null;
String uniqueID = uniqueAccountIdentifier(credentials);
synchronized (SHARED_ACCOUNT_HASHMAP) {
if (SHARED_ACCOUNT_HASHMAP.containsKey(uniqueID)) {
account = SHARED_ACCOUNT_HASHMAP.get(uniqueID);
log("...retrieved Shared Account object: %s", uniqueID);
}
// create the Account object (if necessary)
if (account == null) {
account = new Account(credentials);
// Store it in the SHARED_ACCOUNT_HASHMAP
SHARED_ACCOUNT_HASHMAP.put(uniqueID, account);
log("...created Account object: %s",uniqueID);
}
}
}
ruby thread programming , ruby equivalent of java wait/notify/notifyAll
With the caveat that I don't know Java, based on your comments, I think that you want a condition variable. Google for "Ruby condition variable" comes up with a bunch of useful pages. The first link I get, seems to be a nice quick introduction to condition vars in particular, while this looks like it gives a much broader coverage of threaded programming in Ruby.
Related Topics
Ruby Gem Not Found Although It Is Installed
What Does :: (Double Colon) Mean in Ruby
Activerecord Select Except Columns
Error While Trying to Load the Gem 'Devise. Activesupport: Duration Can't Be Coerced into Integer
Ruby Dynamic Classes. How to Fix "Warning: Class Variable Access from Toplevel"
Dynamically Creating a Multi-Dimensional Hash in Ruby
How to Install Nokogiri Ruby Gem with Mkmf.Log Saying Libiconv Not Found
How to Get a Date from a Week Number
Get Path to Activestorage File on Disk
Ruby - Keyword Arguments - Can You Treat All of the Keyword Arguments as a Hash? How
How to Order Files by Last Modified Time in Ruby
Why Bundle Install Is Installing Gems in Vendor/Bundle
Rails 3. How to Add a Helper That Activeadmin Will Use