??????????????
package com.linuxidc.base.threadTest;
public class SynchronizedTest implements Runnable{
private static int m=0;
public static void main(String[] args) {
Thread thread1=new Thread(new SynchronizedTest());
Thread thread2=new Thread(new SynchronizedTest());
thread1.start();
thread2.start();
try {
//join() ?main????????????????н???????????????????
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m????????"+m);
}
public synchronized void run() {
for(int i=0;i<10000;i++){
m++;
}
}
}
??????δ???????н???????????main????????????????Runnable?????????????????????????и????Runnable??????????thread1????????????????????????????????????thread2?????????????????????????????????????????в??????????????????????????????
?????????????
package com.linuxidc.base.threadTest;
public class SynchronizedTest implements Runnable{
private static int m=0;
public static void main(String[] args) {
Thread thread1=new Thread(new SynchronizedTest());
Thread thread2=new Thread(new SynchronizedTest());
thread1.start();
thread2.start();
try {
//join() ?main????????????????н???????????????????
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m????????"+m);
}
public void run() {
for(int i=0;i<10000;i++){
count();
}
}
public static synchronized void count(){
m++;
}
}
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Σ????????????????????