本文引自:https://www.cnblogs.com/chenpi/p/5159558.html
package com.pichen.java.static_; public class StaticTest { private static int s = 0; public static int sum(int n){ s = 0; for(int i = 0; i <= n; i++){ s += i; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } return s; } }
package com.pichen.java.static_;
public class ThreadCount implements Runnable{
@Override
public void run() {
while(true){
System.out.println(Thread.currentThread().getName() +":" +StaticTest.sum(100));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.pichen.java.static_; public class Main { public static void main(String[] args) { ThreadCount t1 = new ThreadCount(); new Thread(t1).start(); ThreadCount t2 = new ThreadCount(); new Thread(t2).start(); ThreadCount t3 = new ThreadCount(); new Thread(t3).start(); } }
关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
原文:https://www.cnblogs.com/gxlaqj/p/13891834.html