package com.company.bingfa;
class MyThread7 extends Thread{
// public static volatile int n;
public static int n;
private static synchronized void increase(){
n++;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
// n = n + 1;
increase();
try {
sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadJoin2 {
public static void main(String[] args) throws InterruptedException {
Thread[] ts = new Thread[100];
for (int i = 0; i < 100; i++) {
ts[i] = new MyThread7();
}
for (int i = 0; i < 100; i++) {
ts[i].start();
}
for (int i = 0; i < 100; i++) {
ts[i].join();
}
System.out.println(MyThread7.n);
}
}
volatile和synchronized对比
猜你喜欢
转载自blog.csdn.net/yongwoozzang/article/details/103579663
今日推荐
周排行