同步课程 [英] syncronized class

查看:77
本文介绍了同步课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了为每个方法应用

[MethodImpl(MethodImplOptions.Synchronized)]属性之外,有没有办法指定类的所有方法都应该是

同步?


格里

解决方案

格里,


否没有。


另外,使用MethodImpl并不是一个好习惯,对于

实例,它使用this用于锁定对方法的访问。

不仅格式不够精细,而且还向外界展示了一个实现细节

(你锁定的对象)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" gerry" < ge ** @ nospam.nospamwrote in message

news:OD ************** @ TK2MSFTNGP06.phx.gbl ...
< blockquote class =post_quotes>
有没有办法指定类的所有方法应该是

同步而不是应用

[MethodImpl(MethodImplOptions)每个方法的.Synchronized)]属性?


格里



谢谢nicholas,


实际上进一步认为锁定''this''正是我想要的 - 我想要锁定对整个班级的任何访问权限

会改变你的答案吗?


静态方法怎么样 - 在这种情况下锁定了什么?


Gerry


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl .. 。


格里,


不,没有。


另外,使用MethodImpl并不是一个好的做法,对于

个实例,它使用this。用于锁定对方法的访问。不仅





不够精细,但是你暴露了一个实现



详细信息


(您锁定的对象)外部世界。

希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" gerry" < ge ** @ nospam.nospamwrote in message

news:OD ************** @ TK2MSFTNGP06.phx.gbl ...
< blockquote class =post_quotes>
有没有办法指定类的所有方法应该是

同步而不是应用

[MethodImpl(MethodImplOptions)每个方法的.synchronized)]属性






Gerry




Gerry,


不,不是。你不想锁定这个原因的原因是它会让某些访问你控制权的人获得相同的锁定机会,并且可能会让你无法访问很容易上课。


使用静态方法,MethodImpl将锁定类型。同样,你不会因为我之前提到的相同原因而想要这样做。你想要什么

就是这样的:


class MyClass

{

private object classLock = new object();


public void DoSomething()

{

lock(classLock)

{

}

}


public void DoSomethingElse()

{

锁定(classLock)

{

}

}

}


对于静态方法,你需要一个不同的锁:


class MyStaticClass

{

私有静态对象classLock = new object();


public static void DoSomething()

{

lock(classLock)< br $>
{

}

}


public static void DoSomethingElse()

{

lock(classLock)

{

}

}

} b / b
- Nicholas Paldino [.NET / C#MVP]

- MV * @垃圾邮件。 guard.caspershouse.com

" gerry" < ge ** @ nospam.nospamwrote in message

news:um ************** @ TK2MSFTNGP03.phx.gbl ...
< blockquote class =post_quotes>
感谢nicholas,


实际上进一步认为锁定''this''正是我想要的 -

i

想要锁定对整个班级的任何访问权限

这会改变你的答案吗?


静态方法怎么样 - 在这种情况下锁定了什么?


Gerry


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.comwrote



消息新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl ...


> Gerry,

不,没有。
此外,使用MethodImpl并不是一个好的做法,对于
实例,它使用this。用于锁定对方法的访问。不仅





>不够精细,但是你暴露了一个实现



详细信息


>(您锁定的对象)到外部世界。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

gerry < ge ** @ nospam.nospamwrote in message
新闻:OD ************** @ TK2MSFTNGP06.phx.gbl ...


除了应用

[MethodImpl(MethodImplOptions.Synchronized)]之外,有没有办法指定类的所有方法应该是

同步每种方法的属性





>

Gerry




Is there any way to specify that all methods of a class should be
synchronized other than applying the
[MethodImpl(MethodImplOptions.Synchronized)] attribute for every method ?

Gerry

解决方案

Gerry,

No, there is not.

Also, it''s not exactly good practice to use MethodImpl, as for
instances, it uses "this" for locking on access to the methods. Not only is
that not fine grained enough, but you are exposing an implementation detail
(the object you are locking on) to the outside world.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"gerry" <ge**@nospam.nospamwrote in message
news:OD**************@TK2MSFTNGP06.phx.gbl...

Is there any way to specify that all methods of a class should be
synchronized other than applying the
[MethodImpl(MethodImplOptions.Synchronized)] attribute for every method ?

Gerry



thanks nicholas,

actually on further thought locking on ''this'' is exactly what i do want - i
want to lock any access to the entire class
does that change your answer any ?

what about static methods - what is locked in that case ?

Gerry

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP02.phx.gbl...

Gerry,

No, there is not.

Also, it''s not exactly good practice to use MethodImpl, as for
instances, it uses "this" for locking on access to the methods. Not only

is

that not fine grained enough, but you are exposing an implementation

detail

(the object you are locking on) to the outside world.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"gerry" <ge**@nospam.nospamwrote in message
news:OD**************@TK2MSFTNGP06.phx.gbl...

Is there any way to specify that all methods of a class should be
synchronized other than applying the
[MethodImpl(MethodImplOptions.Synchronized)] attribute for every method

?


Gerry




Gerry,

Nope, it does not. The reason you don''t want to lock on this is that it
will give someone accessing your control access to the same locking
mechaism, and could starve access to your class easily.

With static methods, MethodImpl will lock on the type. Again, you don''t
want to do that for the same reasons I mentioned before. What you want to
do is something like this:

class MyClass
{
private object classLock = new object();

public void DoSomething()
{
lock (classLock)
{
}
}

public void DoSomethingElse()
{
lock (classLock)
{
}
}
}

For static methods, you want a different lock:

class MyStaticClass
{
private static object classLock = new object();

public static void DoSomething()
{
lock (classLock)
{
}
}

public static void DoSomethingElse()
{
lock (classLock)
{
}
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"gerry" <ge**@nospam.nospamwrote in message
news:um**************@TK2MSFTNGP03.phx.gbl...

thanks nicholas,

actually on further thought locking on ''this'' is exactly what i do want -
i
want to lock any access to the entire class
does that change your answer any ?

what about static methods - what is locked in that case ?

Gerry

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in
message news:%2****************@TK2MSFTNGP02.phx.gbl...

>Gerry,

No, there is not.

Also, it''s not exactly good practice to use MethodImpl, as for
instances, it uses "this" for locking on access to the methods. Not only

is

>that not fine grained enough, but you are exposing an implementation

detail

>(the object you are locking on) to the outside world.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"gerry" <ge**@nospam.nospamwrote in message
news:OD**************@TK2MSFTNGP06.phx.gbl...

Is there any way to specify that all methods of a class should be
synchronized other than applying the
[MethodImpl(MethodImplOptions.Synchronized)] attribute for every method

?

>
Gerry





这篇关于同步课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆