【发布时间】:2015-12-30 02:16:38
【问题描述】:
c# 是否有自己的 java“同步”关键字版本?
即在 java 中,它可以指定为函数、对象或代码块,如下所示:
public synchronized void doImportantStuff() {
// dangerous code goes here.
}
或
public void doImportantStuff() {
// trivial stuff
synchronized(someLock) {
// dangerous code goes here.
}
}
【问题讨论】:
-
块形式需要引用锁。在方法形式中,锁定对象是隐含的 this(或者静态方法的 Class [this.class,not getClass()],但不要锁定 Classes)。
-
仍然不受保护?我总是来这里,因为我不记得那条
[MethodImpl(MethodImplOptions.Synchronized)]行了。 -
我认为你的第二个 sn-p 不会编译 - 它需要在某些东西上同步。
标签: c# java multithreading synchronization