Java??????????
???????????? ???????[ 2016/2/23 10:29:17 ] ??????????????? ????
????????
?????????????????????????????????????????????ü??????????????‘????’???????????????????Synchronized?????????????????????????????????????????????????????????????????????????飬????????????????????????????????????
????Synchronized?????
????Java??????????????????????????????????????????????????????????????????????и?δ???
????????????????????????????object?е????synchronized(this)??????????????????????????????????С?????????????????????????????????????????и????顣
????????????????????object?????synchronized(this)??????????????????????????????object?е??synchronized(this)???????顣
??????????????????????????object?????synchronized(this)???????????????????object??????????synchronized(this)?????????????????????
????????????????????????????????顣?????????????????object?????synchronized(this)?????????????????????object???????????????????????object?????????????????????????????????
?????????????????????????????.
???????????
package test160118; public class TestSynchronized { public static void main(String[] args) { Sy sy = new Sy(0); Sy sy2 = new Sy(1); sy.start(); sy2.start(); } } class Sy extends Thread { private int flag ; static Object x1 = new Object(); static Object x2 = new Object(); public Sy(int flag) { this.flag = flag; } @Override public void run() { System.out.println(flag); try { if (flag == 0) { synchronized (x1) { System.out.println(flag+"?????x1"); Thread.sleep(1000); synchronized (x2) { System.out.println(flag+"?????x2"); } System.out.println(flag+"?????x1??x2"); } } if(flag == 1) { synchronized (x2) { System.out.println(flag+"?????x2"); Thread.sleep(1000); synchronized (x1) { System.out.println(flag+"?????x1"); } System.out.println(flag+"?????x1??x2"); } } } catch (InterruptedException e) { e.printStackTrace(); } } }
???????
??????????????????????????????????????????????????????????????????????е?????????????????????????ò???????????д????????
??????
???·???
??????????????????
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