假设多个线程共享一个静态变量,如果让每个线程都执行相同的方法每次让静态变量自增1,这样的做法线程安全吗?能保证自增变量数据同步吗?本篇体验使用lock语句块和Interlocked类型方法保证自增变量的数据同步. □ 线程不安全.数据不同步的做法 class Program { static int sum = 0; static void Main(string[] args) { Stopwatch watch = new Stopwatch(); watch.Start(); Parall
静态只能调用静态 非静态: 对象名.方法名 package ti; //通过两个类 StaticDemo.LX4_1 说明静态变量/方法与实例变量/方法的区别. class StaticDemo { static int x; int y; public static int getX() { return x;//静态方法中可以访问静态数据成员x } public static void setX(int newX) { x = newX; } public int getY() {//int