“synchronized”java关键字的对应部分? [英] Counterpart of the 'synchronized' java keyword?

查看:72
本文介绍了“synchronized”java关键字的对应部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,''锁定'是唯一的选择,不是吗?


即使我想宣布''同步'''方法,我这样做:


A级{


void AMethod(){

lock(这个){

//方法体

}

}


}


我是对的吗?


DW

解决方案

DW,


是的,你就是这样做的,虽然你可以选择不同的对象来锁定除this之外的其他对象。


你也可以将MethodImpl属性附加到方法中,如下所示:


class A

{

[MethodImpl(MethodImplOptions.Synchronized)]

void AMethod()

{

}

}


这与将方法锁定在this上的效果相同。


希望这会有所帮助。


master <毫安**** @ master.com>在留言中写道

新闻:OZ ************** @ TK2MSFTNGP02.phx.gbl ...

据我所知,''lock''是唯一的选择,不是吗?

即使我想宣布''synchronized''方法,我这样做:

A级{

void AMethod(){
锁定(本){
//方法体
}
}

}

我是对的吗?

DW



master写道:

据我所知,''锁''是唯一的选择,不是吗?

即使我想宣布一个''synchronized''方法,我这样做:

A类{

void AMethod(){
锁定(这){
//方法组
}
}


我是对的吗?

DW




你好DW,


是的。


-

希望这会有所帮助,

Tom Spink


>您还可以将MethodImpl属性附加到方法,如下所示:


[MethodImpl(MethodImplOptions.Synchronized)]
void AMethod()




谢谢,我不知道。


DW


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 body
}
}

}

Am I right?

DW

解决方案

DW,

Yes, that is how you do it, although you can choose different objects to
lock on other than "this".

You can also attach the MethodImpl attribute to the method, like so:

class A
{
[MethodImpl(MethodImplOptions.Synchronized)]
void AMethod()
{
}
}

This has the same effect as locking the method on "this".

Hope this helps.

"master" <ma****@master.com> wrote in message
news:OZ**************@TK2MSFTNGP02.phx.gbl...

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 body
}
}

}

Am I right?

DW



master wrote:

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 body
}
}

}

Am I right?

DW



Hi DW,

Yes.

--
Hope this helps,
Tom Spink


> You can also attach the MethodImpl attribute to the method, like so:


[MethodImpl(MethodImplOptions.Synchronized)]
void AMethod()



Thanks, I did not know about it.

DW


这篇关于“synchronized”java关键字的对应部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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