????????????????
???????????????????????????????????????????????Ч???????????????????????(??????)?????????????????????????н???????????????????????????κ????塣
??????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????
??????????????m??????????????????????????????????????10000?Σ????????????????20000???????????????????????????
package com.linuxidc.base.threadTest;
public class SynchronizedTest implements Runnable{
private static volatile int m=0;
public static void main(String[] args) {
Runnable run=new SynchronizedTest();
Thread thread1=new Thread(run);
Thread thread2=new Thread(run);
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++){
m++;
}
}
}
???????????ж???? m????С??20000??????????????????????????thread1???m++????д????????????thread2?????????ж????m??????????????????????????++????????m=1д??????У??????????thread1?????m=1???????????????thread1??????thread2??????????????????????????
??????ο?????????????????????????????????????????“???л???????????”????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Java?У???????????????????????????synchronized??Lock??
????????????????synchronized?????????????????飩
??????????????????壬????????????????
????????????????????????????????????????????????????????????????????????????
??????Java?У?????????????????????monitor??????????????????????????????????????????и??????????????????
??????Java?У????????synchronized???????????????????????????????????飬????????????????synchronized???????????synchronized????????????????????????????????????????????????????????е????????????????????????????????????????????????????????????????????????????顣?????????????????????????????????????????????????????
????synchronized?÷???
????1??????????
????synchronized(synObject) {
????}
?????÷?????synchronized???????????????????????????????????????????????δ???飬??????????????????synObject?????????????????????????У?????μ?????????????????????????????????????顣
package com.linuxidc.base.threadTest;
public class SynchronizedTest implements Runnable{
private static volatile int m=0;
public static void main(String[] args) {
Runnable run=new SynchronizedTest();
Thread thread1=new Thread(run);
Thread thread2=new Thread(run);
thread1.start();
thread2.start();
try {
//join() ?main????????????????н???????????????????
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m????????"+m);
}
public void run() {
synchronized (this) {
for(int i=0;i<10000;i++){
m++;
}
}
}
}
???????????????????????????????????????????????????????????????
package com.linuxidc.base.threadTest;
public class SynchronizedTest implements Runnable{
private static volatile int m=0;
private Object object=new Object();
public static void main(String[] args) {
Runnable run=new SynchronizedTest();
Thread thread1=new Thread(run);
Thread thread2=new Thread(run);
thread1.start();
thread2.start();
try {
//join() ?main????????????????н???????????????????
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("m????????"+m);
}
public void run() {
synchronized (object) {
for(int i=0;i<10000;i++){
m++;
}
}
}
}
????2?????????
package com.linuxidc.base.threadTest;
public class SynchronizedTest implements Runnable{
private static int m=0;
public static void main(String[] args) {
Runnable run=new SynchronizedTest();
Thread thread1=new Thread(run);
Thread thread2=new Thread(run);
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++;
}
}
}
??????δ????У?synchronzied???????????????????????????????run()????????????????????????????????ж??????????run?????????????????濴?????δ?????main???????????????????????Runnable?????????????????????????????????Runnable???????????????????????????????У?????????????????????????????