本文实例分析了java多线程中的volatile和synchronized用法。分享给大家供大家参考。具体实现方法如下:
public class Volatiletest extends Thread {
private static int count = 0;
public void run() {
count++;
}
public static void main(String[] args) {
Thread threads[] = new Thread[10000];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Volatiletest();
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(count);
}
}
本文地址:https://www.stayed.cn/item/1930
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我