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

    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. Mysql安装(Mac)

    1.安装mysql(百度详解) 2.打开终端 3.输入vim ~/.bash_profile 4.在最后加上PATH=$PATH:/usr/local/mysql/bin 5.按esc,然后输入 :w ...

  2. Ubuntu安装SHH服务

    1.打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--> ...

  3. IDEA总是启动不了

    时常怎么都打不开这个软件,或者很久很久才打开. 解决办法:在任务管理器将IDEA结束进程,再去打开软件,就可以了.

  4. Maven入门(二)pom.xml和核心概念

    一.pom.xml文件说明 1.pom意思就是project object model. 2.pom.xml包含了项目构建的信息,包括项目的信息.项目的依赖等. 3.pom.xml文件是可以继承的,大 ...

  5. 使用nexus 搭建本地 maven 服务器

    1.下载安装nexus oos https://www.sonatype.com/download-oss-sonatype ,选择2.x 版本. 2. 配置maven <server> ...

  6. DP小题集

    P2736 "破锣摇滚"乐队 Raucous Rockers 你刚刚继承了流行的"破锣摇滚"乐队录制的尚未发表的N(1 <= N <= 20)首歌的 ...

  7. Applese 的毒气炸弹(最小生成树)

    链接:https://ac.nowcoder.com/acm/contest/330/G 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言5242 ...

  8. php设计模式总结2

    策略模式: 定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 封装:把行为用接口封装起来,我们可以把那些经常变化的部分,从当前的类中单独取出来,用接口进行单 ...

  9. 小程序自定义modal弹窗封装实现

    前言小程序官方提供了 wx.showModal 方法,但样式比较固定,不能满足多元化需求,自定义势在必行~ 老规矩先上图 点击某个按钮,弹出 modal框,里面的内容可以自定义,可以是简单的文字提示, ...

  10. idea(1)-idea初装

    1.安装插件 Alibaba Java Coding Guidelines Free Mybatis plugin MyBatis Log Plugin Lombok pluginGsonFormat ...