class hehe implements Runnable {
static int count = 0;
public synchronized void count() {
count++;
}
public void run() {
for (int i = 0; i < 10000; i++) {
count();
}
}
}
public class Sychronise {
public static void main(String[] args) {
Thread a1 = new Thread(new hehe());
Thread a2 = new Thread(new hehe());
a1.start();
a2.start();
try {
a1.join();
a2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(hehe.count);
}
}
カウントは20000ですが、出力はまだ不安定です。それは、個々の「笑」のインスタンスで同期だとしてJavaスレッドは同期しています(出力は安定しません)
それは動作します。どうもありがとうございました。 – Varrian
は、私はそれが静的にする必要があり、それが作品 – Tibrogargan