同步两种方法 [英] Synchronize two methods

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

问题描述

大家好,

有什么办法可以同步两种方法?示例:

Hi everybody,

Is there any way to synchronize two methods? Example:

public class myclass
{
     private int x;
     public void method1()
     {
          //Access to x from thread1
     }

     public void method2()
     {
          //Access to x from thread2
     }
}



为此,method1()和method2()将从其他线程访问x.有什么方法可以使它们同步(一次只有一种方法可以访问变量x)?



There for, method1() and method2() will access to x from other threads. Is there any way to synchronize them (just one method can access variable x at a time)?

推荐答案

是的.查看锁定 [
Yes. Look at the Lock[^] statement - that is exactly what it is there for.


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

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