静态字段被类的所有实例所共享,即此类的所有实例都访问同一内存地址。 所以该内存位置的值变更的话,这种变更对所有的实例都可见。

    class MyClass
{
int number = ;
static int staticNumber = ; public void SetValue(int value1, int value2)
{
this.number = value1;
staticNumber = value2;
} public string DisplayValues()
{
return string.Format("number:{0}, staticNumber:{1}", this.number, staticNumber);
}
}
    class Program
{
static void Main(string[] args)
{
MyClass class1 = new MyClass();
MyClass class2 = new MyClass(); class1.SetValue(, ); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); class2.SetValue(, ); Console.WriteLine("Values in class2 are: {0}", class2.DisplayValues()); // 再次显示 class1 实例,发现 staticNumber 的值是 30.
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues());
}
}

进一步,如果有多个线程同时访问静态字段,并对其赋值,那么会出现什么样的情况呢?

(由于进程是一组资源,而进程中的多个线程会共享进程中的资源。)

实际操作发现,对int 字段的访问非常快,不会出现资源抢夺问题。

   public class Worker
{
// Volatile is used as hint to the compiler that this data member will be accessed by multiple threads.
private volatile bool shouldStop; private MyClass class3 = new MyClass(); // This method will be called when the thread is started.
public void DoWork()
{
while (!shouldStop)
{
Console.WriteLine("Worker thread: working and setting values.");
class3.SetValue(, );
} Console.WriteLine("Worker thread: terminating gracefully...");
} public void RequestStop()
{
this.shouldStop = true;
}
}
static void Main(string[] args)
{
MyClass class1 = new MyClass();
MyClass class2 = new MyClass(); class1.SetValue(, ); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); class2.SetValue(, ); Console.WriteLine("Values in class2 are: {0}", class2.DisplayValues()); // 再次显示 class1 实例,发现 staticNumber 的值是 20.
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); // Create the thread object.
Worker worker1 = new Worker();
Thread workerThread = new Thread(worker1.DoWork); // Start the worker thread.
workerThread.Start(); // Loop until worker thread acivates.
while (!workerThread.IsAlive) ; // Put the main thread to sleep for a while to allow worker thread do some work.
Thread.Sleep(); // Set values by main thread.
class1.SetValue(, );
Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues()); // Request the worker thread to stop.
worker1.RequestStop(); // Use the Join method to block the current thread until the object's thread terminates.
workerThread.Join();
Console.WriteLine("Main thread: Worker thread has terminated."); Console.WriteLine("Values in class1 are: {0}", class1.DisplayValues());
}

如果静态字段是一个非托管资源,会怎么样呢?

(C# 基础) 静态字段,静态类,静态方法。的更多相关文章

  1. 零基础学Java(12)静态字段与静态方法

    静态字段与静态方法   之前我们都定义的main方法都被标记了static修饰符,那到底是什么意思?下面我们来看看 静态字段   如果将一个字段定义为static,每个类只有一个这样的字段.而对于非静 ...

  2. C#学习笔记----静态字段和静态方法

    1.使用关键字 static 修饰的字段或方法成为静态字段和静态方法,如 public static int num = 1;2.静态字段属于类,并为类所用.而非静态字段属于对象,只能被特定的对象专有 ...

  3. (10)C#静态方法,静态字段,静态类,匿名类

    6.静态方法 使用静态方法就可不必用类的实例化调用次函数 class Test { public static void method() { ........ } //当调用一个method()时就 ...

  4. Python学习:16.Python面对对象(三、反射,构造方法,静态字段,静态方法)

    一.构造方法 在使用类创建对象的时候(就是类后面加括号)就自动执行__init__方法. class A: def __init__(self): print('A') class B: def __ ...

  5. 深入理解 静态类和静态字段(C# 基础)

    序言 以前,总是被提醒,在编程过程中尽量少用静态变量,数据丢失什么的,今天有空,禁不住对静态变量的强烈好奇,跟我一起了解下静态家族的内幕吧. 静态类 定义 静态类与非静态类的重要区别在于静态类不能实例 ...

  6. JAVA的静态变量、静态方法、静态类

    静态变量和静态方法都属于静态对象,它与非静态对象的差别需要做个说明. (1)Java静态对象和非静态对象有什么区别? 比对如下: 静态对象                                ...

  7. 关于Java的静态:静态类、静态方法、静态变量、静态块等

    原文地址:Java static keyword - Class, Method, Variable, Block, import - JournalDev 很少看到文章能把静态这个问题解释的很清楚, ...

  8. 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法

    self是什么,就是一个函数,就是一个形式参数 4.self就是调用当前方法的对象 静态字段,公有属性 静态字段使用场景,每个对象中保存相同的东西时,可以使用静态字段,公有属性 5.封装的理解 类中封 ...

  9. java静态方法和静态字段

    public class Dog{ public static void main(String[]args){ A a= new A(); a.add(); //java实例对象可以访问类的静态方法 ...

随机推荐

  1. js 时间日期大小对比

    var oDate1 = new Date(); var oDate2 = new Date("2019/01/07 10:00:00"); if (oDate1.getTime( ...

  2. Mysql tips 功能...

    1. mysql  GROUP_CONCAT() 使用 排序... SELECT shop.id, shop.name, shop.user_id, shop.address, shop.map_lo ...

  3. Go语言基础之3--时间和日期序列

    一.时间和日期类型 1. time包 2. time.Time类型,用来表示时间 3. 获取当前时间, now := time.Now() 实例1-1  打印输出当前时间 package main i ...

  4. spring boot 启动时运行代码(2)ApplicationListener

    项目概览: StepExecutor: @Component @Slf4j public class StepExecutor implements Runnable { @Autowired pri ...

  5. runlevel:启动运行级别(3-13)

    0:halt 关机模式1:single user 单用户2:Multiuser 多用户3:Full multiuser mode 命令行模式4:unused 没有使用5:Xll 桌面模式6:reboo ...

  6. phpstrom的xdebug开启和yii2下的分页的链接

    phpstrom的xdebug开启 1.修改php.ini文件(修改完重启apaceh) xdebug.remote_enable = onxdebug.idekey= PHPSTROM [注意:远程 ...

  7. Angular JS ng-model对<select>标签无效的情况

    使用场景一: <select ng-if="item.award_type==1" id="award{{$index+1}}" name="X ...

  8. app内部H5测试点总结

    1.业务逻辑 除基本功能测试外,需要关注的一些测试点: a.登录 a.1 H5页面嵌入到客户端使用,若客户端已经登录,进入H5页面应该是登录状态 a.2 H5页面嵌入到客户端内使用,若客户端未登录,如 ...

  9. tcp发送缓冲区学习

    https://blog.csdn.net/ysu108/article/details/7764461 Nginx 模块开发书上有内容   陶辉博客也可以

  10. Zookeeper的集群配置和Java测试程序 (一)

    概述 Zookeeper是Apache下的项目之一,倾向于对大型应用的协同维护管理工作.IBM则给出了IBM对ZooKeeper的认知: Zookeeper 分布式服务框架是 Apache Hadoo ...