Java:synchronized???????????????
???????????? ???????[ 2012/11/19 9:50:28 ] ????????
????????????????synchronized?????????????С????ε??Χ??????С???????????????????????????????????????м?С????????????????????????????????????????????????????????????????????????????????л?????????????????????????????????????
??????????????????????ж?new?????Sync??????????????????Sync????????????????????????????????????synchronized?????????Ρ?
???????????????????????′???????????????????Sync?????
class MyThread extends Thread {
private Sync sync;
public MyThread(Sync sync) {
this.sync = sync;
}
public void run() {
sync.test();
}
}
public class Main {
public static void main(String[] args) {
Sync sync = new Sync();
for (int i = 0; i < 3; i++) {
Thread thread = new MyThread(sync);
thread.start();
}
}
}
???????н????
????test???..
????test????..
????test???..
????test????..
????test???..
????test????..
??????????????????synchronized??????synchronized????ζ????????á?
??????????????????????δ????????????????????????????δ?????????new???Sync?????????????test???????????????С?
??????????????????????????????????磬synchronized??????????????static final??????????????????????????????????????????synchronized???????????Class????
class Sync {
public void test() {
synchronized (Sync.class) {
System.out.println("test???..");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test????..");
}
}
}
class MyThread extends Thread {
public void run() {
Sync sync = new Sync();
sync.test();
}
}
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
Thread thread = new MyThread();
thread.start();
}
}
}
???????н????
????test???..
????test????..
????test???..
????test????..
????test???..
????test????..
?????????????synchronized(Sync.class)????????????Ч????
????????static synchronized???????static??????????????????????????????????????this????????????static synchronized????????????????????????????Ρ?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11