473,799 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static lock

in java if I was calling a synchronized block and wanted to sync it even for
static blocks of code I could do a synchronize(the Object.getClass ()), can I
do a lock(theObject. GetType()) on C# and will it lock for static blocks of
code that use that object type?
Nov 15 '05 #1
4 10694
ok im a retard, I just read hte msdn documentation :-)

"memememe" <[rem]casolorz[rem]@hot[rem]mail.com> wrote in message
news:4Z2kb.1471 3$iq3.8102@okep read01...
in java if I was calling a synchronized block and wanted to sync it even for static blocks of code I could do a synchronize(the Object.getClass ()), can I do a lock(theObject. GetType()) on C# and will it lock for static blocks of
code that use that object type?

Nov 15 '05 #2
memememe <[rem]casolorz[rem]@hot[rem]mail.com> wrote:
in java if I was calling a synchronized block and wanted to sync it even for
static blocks of code I could do a synchronize(the Object.getClass ()), can I
do a lock(theObject. GetType()) on C# and will it lock for static blocks of
code that use that object type?


Note that even if you've just found the answer, I'd suggest *not*
locking on the type in either Java or C# - it could cause problems if
other classes also lock on the type.

Instead, have a private member which you lock on. So, for a static
lock:

private static Object padlock = new Object();

....

lock (padlock)
or
synchronized (padlock)
(Note that the "private" modifier is unnecessary in C#.)

I'd suggest doing the same for instance locks as well. That way you
know nothing else is going to be locking on the same reference, as
nothing *has* the same reference. It also means you can use finer-
grained locks simply.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
not to mention that quite unintuitively, the GetType() operation is very
costly.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de
news:MP******** *************** *@msnews.micros oft.com...
memememe <[rem]casolorz[rem]@hot[rem]mail.com> wrote:
in java if I was calling a synchronized block and wanted to sync it even for static blocks of code I could do a synchronize(the Object.getClass ()), can I do a lock(theObject. GetType()) on C# and will it lock for static blocks of code that use that object type?


Note that even if you've just found the answer, I'd suggest *not*
locking on the type in either Java or C# - it could cause problems if
other classes also lock on the type.

Instead, have a private member which you lock on. So, for a static
lock:

private static Object padlock = new Object();

...

lock (padlock)
or
synchronized (padlock)
(Note that the "private" modifier is unnecessary in C#.)

I'd suggest doing the same for instance locks as well. That way you
know nothing else is going to be locking on the same reference, as
nothing *has* the same reference. It also means you can use finer-
grained locks simply.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
> I'd suggest doing the same for instance locks as well. That way you
know nothing else is going to be locking on the same reference, as
nothing *has* the same reference. It also means you can use finer-


As a note, if you will be supplying a "SyncRoot" property (e.g using
ICollection, IList, etc.) for your object, then it probably does not matter
if you use "this" or another internal object as any class that can gain
access to your object ref could also get access to SyncRoot and lock either
or both. So the net effect would be the same if using one lock. You will
notice most (if not all) of the Framework classes that derive from
ICollection and return a SyncRoot (i.e. Array, Queue, Stack, ArrayList,
HashTable, etc.) just return "this" as the sync object probably for this
reason. If you don't need or want to expose a sync object, then I agree a
private object is probably better.

--
William Stacey, MVP
Nov 15 '05 #5

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

Similar topics

9
1981
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and interfere with one another. My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also uses static methods for its data access calls - for example execute...
6
8307
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream fStream=null; lock(fStream) //just in case we use it for multithreading to be thread safe {
10
11916
by: McFly Racing | last post by:
Thread Locking In Static Methods I have the need for a Log Manger class that has static methods. Normally I would use the lock statement or a Monitor statement both of which take a reference to (this). In the case of these static methods I am not able to do that.
9
4162
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I had to add a call to a web service, which in
6
1445
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code can be found in Project SystemFramework within module ApplicationLog.cs. Here comes the sample. namespace Duwamish7.SystemFramework { (some code...)
2
5485
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto synchronized ? Regards Vivek Ragunathan
7
5699
by: FredC | last post by:
I need some insight into timers, the static modifier and instance memory safety. public class myClass { protected static int i = 0; portected float myfloat; protected static Timer staticTimer = new Timer(); } public class myObject: myClass
8
2629
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new Object(); ...// some codes
4
4531
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.