(C# 基础) 静态字段,静态类,静态方法。
静态字段被类的所有实例所共享,即此类的所有实例都访问同一内存地址。 所以该内存位置的值变更的话,这种变更对所有的实例都可见。
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# 基础) 静态字段,静态类,静态方法。的更多相关文章
- 零基础学Java(12)静态字段与静态方法
静态字段与静态方法 之前我们都定义的main方法都被标记了static修饰符,那到底是什么意思?下面我们来看看 静态字段 如果将一个字段定义为static,每个类只有一个这样的字段.而对于非静 ...
- C#学习笔记----静态字段和静态方法
1.使用关键字 static 修饰的字段或方法成为静态字段和静态方法,如 public static int num = 1;2.静态字段属于类,并为类所用.而非静态字段属于对象,只能被特定的对象专有 ...
- (10)C#静态方法,静态字段,静态类,匿名类
6.静态方法 使用静态方法就可不必用类的实例化调用次函数 class Test { public static void method() { ........ } //当调用一个method()时就 ...
- Python学习:16.Python面对对象(三、反射,构造方法,静态字段,静态方法)
一.构造方法 在使用类创建对象的时候(就是类后面加括号)就自动执行__init__方法. class A: def __init__(self): print('A') class B: def __ ...
- 深入理解 静态类和静态字段(C# 基础)
序言 以前,总是被提醒,在编程过程中尽量少用静态变量,数据丢失什么的,今天有空,禁不住对静态变量的强烈好奇,跟我一起了解下静态家族的内幕吧. 静态类 定义 静态类与非静态类的重要区别在于静态类不能实例 ...
- JAVA的静态变量、静态方法、静态类
静态变量和静态方法都属于静态对象,它与非静态对象的差别需要做个说明. (1)Java静态对象和非静态对象有什么区别? 比对如下: 静态对象 ...
- 关于Java的静态:静态类、静态方法、静态变量、静态块等
原文地址:Java static keyword - Class, Method, Variable, Block, import - JournalDev 很少看到文章能把静态这个问题解释的很清楚, ...
- 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法
self是什么,就是一个函数,就是一个形式参数 4.self就是调用当前方法的对象 静态字段,公有属性 静态字段使用场景,每个对象中保存相同的东西时,可以使用静态字段,公有属性 5.封装的理解 类中封 ...
- java静态方法和静态字段
public class Dog{ public static void main(String[]args){ A a= new A(); a.add(); //java实例对象可以访问类的静态方法 ...
随机推荐
- 树链剖分【洛谷P4114】 Qtree1
P4114 Qtree1 题目描述 给定一棵n个节点的树,有两个操作: CHANGE i ti 把第i条边的边权变成ti QUERY a b 输出从a到b的路径中最大的边权,当a=b的时候,输出0 码 ...
- 「BZOJ1038」「洛谷P2600」「ZJOI2008」瞭望塔 半平面交+贪心
题目链接 BZOJ/洛谷 题目描述 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安. 我们将H村抽象为一维的轮廓.如下图所示: 我们可以用一条山的上方 ...
- C++并发低级接口:std::thread和std::promise
std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...
- 10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)
Level: Easy 题目描述: Given a string s and a non-empty string p, find all the start indices of p's ana ...
- 理解Javascript_01_理解内存分配
理解Javascript_01_理解内存分配 转载自:http://www.cnblogs.com/fool/archive/2010/10/07/1845226.html 在正式开始之前,我想先 ...
- 百度地图 api bug 解决.......
百度地图 遇到了一个默明奇妙的bug..... 调用后中心点 不再 point(标注的点上...)这是需要执行一次(仅一次) 当 地图 加载完后 执行(这个方法你每次改地图 都会执行...所以让他执 ...
- Html背景图
<table style="height: 210px;" ><tbody><tr><td style="background- ...
- hau1021 Fibonacci Again(递归)
Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 在U盘打造个性化PE工具箱+KALI(Persistence)+存储的工作站
基本工具: kali-linux-2018.2-amd64 原版镜像:https://www.kali.org/downloadsWin32DiskImager yunfile 下载较慢,建议自行百度 ...
- Device eth0 does not seem to be present, delaying initialization: Linux Networking
copy centos 报错 Device eth0 does not seem to be present, delaying initialization: Linux Networking # ...