- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我知道如何在我的应用程序中使用锁,但仍然有几件事我不太了解锁定(顺便说一句 - 我知道 lock 语句只是使用 Monitor 类类型的速记符号)。
来自 http://msdn.microsoft.com/en-us/library/ms173179.aspx :
public class TestThreading
{
private System.Object lockThis = new System.Object();
public void Function()
{
lock (lockThis)
{
// Access thread-sensitive resources.
}
}
}
The argument provided to the lock keyword must be an object based on a reference type, and is used to define the scope of the lock. In the example above, the lock scope is limited to this function because no references to the object lockThis exist outside the function. If such a reference did exist, lock scope would extend to that object.
- The scope of the lock, in this situation, is the individual instance of the class itself. This is opposed to crossing all instance of the specific class. You could have your lock cross all instances TestThreading by making lockThis static. That's what's meant by the "scope" of the lock: whether it's applicable to a single instance, or applicable to every instance of a particular type.
Other objects could still access lockThis from another thread, but they wouldn't be able to process code that is surrounded by a lock on that object.
In response to your response: There is a significance. If TestThreading.Function is static, then lockThis has to be static, otherwise, TestThreading.Function cannot access lockThis at all.
public class TestThreading
{
private System.Object lockThis = new System.Object();
public static void Function()
{
lock (new TestThreading().lockThis)
{
// Access thread-sensitive resources.
}
}
}
The scope of the lock, in this situation, is the individual instance of the class itself. This is opposed to crossing all instance of the specific class. You could have your lock cross all instances TestThreading by making lockThis static.
No, if Function is static, then all TestThread instances would share the same room, if lockThis is static, then all TestThread instances would share the same key.
The room is the code that is executed within the lock, not the lockThis object itself, that's simply the key.
最佳答案
I’m not sure what you mean by TestThreading.Function not being able to access lockThis?!
public class TestThreading
{
private object lockThis = new object();
public static void Function()
{
lockThis = new object();
lock (lockThis)
{
// access something
}
}
}
if the lockThis was static, then all TestThread instances would share the same room (aka lock), even though TestThread.Function isn’t static. Assuming I understand it correctly now, then the term lock is referring to the key (thus lock doesn’t refer to the room?! ) used for opening the doors?
lock (key)
{
// room
}
As such, if we assume that lockThis is static and TestThread.Function is not static, then the scope of the key/lock is all TestThread instances, which means that all TestThread instances share the same key and thus when one TestThread instance opens the door to its room using this key, the other instances won’t be able to open the doors to their rooms until first instance releases that key?
关于c# - 关于锁的一些令人困惑的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2358614/
我有一个包含两个项目的解决方案,每个项目都生成一个单独的 dll,供另一个 Windows 应用程序使用。这些项目中的每一个都有一个名为 MyActions 的类,只有一个这样的方法 项目 1
我有一个包含两个项目的解决方案,每个项目都生成一个单独的 dll,供另一个 Windows 应用程序使用。这些项目中的每一个都有一个名为 MyActions 的类,只有一个这样的方法 项目 1
所以我在 if 语句中有这段代码如下 if (!inTime || !moment(inTime).format('m') % 15 === 0) { doSomething(); } 传入的 inT
像往常一样,我想做的比我知道的还多:-D 这就是我正在做的事情......我正在写一篇简历。 但是在简介中,我想要一个“长简介”和一个“短简介”按钮。 长传记显然会显示整个传记,但短传记会捕获列表中的
我正在使用物质。 js创建一个二维场景。我在场景中对一个物体施加力,这个物体撞击其他物体,但最终所有物体都因摩擦和能量损失而停止移动。 我需要以某种方式检测场景中的所有物体何时停止移动。我发现这样
谁能快速浏览一下这段代码,让我知道哪里出错了。 在模糊事件中,.textok 类加载正常,但 .textbad 类加载不正常。 .textok { color:#0F0; background
我的情况是这样的:我有一个项目,它使用了一些生成的代码。在生成的代码中,几乎所有文件中都硬编码了某个 URI。 因此,在某些时候我得到了两个生成的代码库:一个针对开发,另一个针对暂存。 我想通过 Gr
这是一个严肃的问题(见我的评论)。 问题很简单:Java 所做的所有 SEO 不友好的事情有哪些会导致您的网站在主要搜索引擎中的排名不如应有的好? 最佳答案 有一个与 JSESSIONID 相关的 s
我正在使用 PHP。我想完成 jQuery AJAX 进程,(完成进程并数据返回主页后)。 然后执行下一个 jQuery 操作。关于如何做到这一点有什么想法吗? $.ajax({ url: "pa
在释放内存之前,我要从 CPU 缓存中逐出内存范围。理想情况下,我只想放弃这些缓存行而不将它们保存到内存中。因为没有人会使用这些值,无论谁再次获得该内存范围(在 malloc()/new/_mm_ma
我不喜欢 jackson 。 我想使用 ajax,但要使用 Google Gson。 所以我试图弄清楚如何实现我自己的 HttpMessageConverter 以将其与 @ResponseBody
我是一名优秀的程序员,十分优秀!