473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Synchronized Hashtable

Hi

I'm using static, synchronized Hashtables:

static Hashtable _htDiags = new Hashtable();
static Hashtable htDiags = Hashtable.Synchronized(_htDiags);

I'm not totally sure, if this protection wrapper is sufficient in a
mutlithreaded Singleton:
if((pi = (ProcessInfo)htDiags[PID]) != null)
pi.MyFunc();
Do I have to additionally lock access to htDiags object, or is
'synchronized' access sufficient? Like this:
lock(htDiags)
{
if((pi = (ProcessInfo)htDiags[PID]) != null)
pi.MyFunc();
}

Thanks for any hints.
Urs
Nov 17 '05 #1
2 4001
The first should be sufficient.

Pete

"Urs Vogel" <uv****@msn.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
Hi

I'm using static, synchronized Hashtables:

static Hashtable _htDiags = new Hashtable();
static Hashtable htDiags = Hashtable.Synchronized(_htDiags);

I'm not totally sure, if this protection wrapper is sufficient in a
mutlithreaded Singleton:
if((pi = (ProcessInfo)htDiags[PID]) != null)
pi.MyFunc();
Do I have to additionally lock access to htDiags object, or is
'synchronized' access sufficient? Like this:
lock(htDiags)
{
if((pi = (ProcessInfo)htDiags[PID]) != null)
pi.MyFunc();
}

Thanks for any hints.
Urs

Nov 17 '05 #2


Urs Vogel wrote:
I'm not totally sure, if this protection wrapper is sufficient in a
mutlithreaded Singleton:
if((pi = (ProcessInfo)htDiags[PID]) != null)
pi.MyFunc();
That would be fine, although you could skip using a Synchronized
singleton and use:

lock ( htDiags.SyncRoot ) {
pi = (ProcessInfo)htDiags[PID];
if ( pi != null )
pi.MyFunc();
}
Do I have to additionally lock access to htDiags object, or is
'synchronized' access sufficient? Like this:
lock(htDiags)
{
if((pi = (ProcessInfo)htDiags[PID]) != null)
pi.MyFunc();
}


The protocol is to lock htDiags.SyncRoot, not htDiags.

If you did a lookup and then mutation:

/***** ALERT, INVALID CODE HERE! ***/
pi = (ProcessInfo)htDiags[PID];
if ( pi == null ) {
pi = f();
htDiags[PID] = pi;
}
pi.MyFunc();

Here the synchronized wrapper of htDiags doesn't help you, and you need:

/* Proper code here */
lock ( htDiags.SyncRoot ) {
pi = (ProcessInfo)htDiags[PID];
if ( pi == null ) {
pi = f();
htDiags[PID] = pi;
}
pi.MyFunc();
}

Be really carefull about using Synchronized wrappers, they only
guarantee synchronization on each seperate method invocation. I prefer
writing the lock explicitly, even when only querying the collection --
if there is multithreading threats.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank | last post by:
Hi, In the javadocs regarding many of the java.util classes, it states that the classes are not synchronized, and suggest using the Collections.synchronizedX(...) methods for getting...
4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
4
by: chrisben | last post by:
Hi I often use Queue.Synchronized method to create a queue for multithread writing. I also know I could use SyncRoot and lock to write Queue. Could anyone here please explain to me the pros and...
6
by: rmunson8 | last post by:
I have a derived class from the Queue base class. I need it to be thread-safe, so I am using the Synchronized method (among other things out of scope of this issue). The code below compiles, but...
8
by: ASP.Net programmer | last post by:
Hi, I have a few methods in a class that I want to synchronize (make sure they can't be used at the same time by multiple threads). As a Java programmer I just do this: public synchronized...
2
by: fred | last post by:
I have created a Collection class inherited from the CollectionBase Class. How would I implement a Synchronized method similar to the Synchronised method of the HashTable Class. Thanks, Fred
0
by: fred | last post by:
I have asked previously how I would implement a Synchronised method similar to the Hashtable Class. I have a class derived from the CollectionBase Class. Nick Hall kindly replied that HashTable...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
10
by: master | last post by:
As far as I understand, 'lock' is the only option, isn't it? Namely, If I want to declare a 'synchronised' method, I do it this way: class A { void AMethod() { lock (this) { // the method...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.