??????????????£??????????????????????????????????????
????1.???????
???????ж?????????е????????
????JVM?????????????????????????????????????????????0????????????????θ????????????????????1???????????????????????????????????????????????
?????????????????????????????????????????????????
????????????????synchronized??????????????????????0??????????????????????????????????????
????2.synchronized?????
????2.1??????????????
?????????????????????????μ?????鶼????????????????????????е?????????????????????Щ????顣
????Resource1.java?????????????????main?????????????????????????????????????У??????Щ????鴦??????????У?????????????????????????????synchronized(this)???????????????????????????
????Resource1.java
????packagecom.zj.lock;
????importjava.util.concurrent.TimeUnit;
????publicclassResource1 {
????publicvoidf() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in f()");
????synchronized(this) {
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in f()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????}
????publicvoidg() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in g()");
????synchronized(this) {
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in g()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????}
????publicvoidh() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in h()");
????synchronized(this) {
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in h()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????}
????publicstaticvoidmain(String[] args) {
????finalResource1 rs =newResource1();
????newThread() {
????publicvoidrun() {
????rs.f();
????}
????}.start();
????newThread() {
????publicvoidrun() {
????rs.g();
????}
????}.start();
????rs.h();
????}
????}
?????????
????Thread-0:not synchronized in f()
????Thread-0:synchronized in f()
????main:not synchronized in h()
????Thread-1:not synchronized in g()
????Thread-0:synchronized in f()
????Thread-0:synchronized in f()
????Thread-0:synchronized in f()
????Thread-0:synchronized in f()
????Thread-1:synchronized in g()
????Thread-1:synchronized in g()
????Thread-1:synchronized in g()
????Thread-1:synchronized in g()
????Thread-1:synchronized in g()
????main:synchronized in h()
????main:synchronized in h()
????main:synchronized in h()
????main:synchronized in h()
????main:synchronized in h()
????2.2??????????????
????Resource1.java?????????????????main?????????????????????????????????????У???Щ????鴦??????????У????????????????????????synchronized(this)??synchronized(syncObject1)??synchronized(syncObject2)??????????????????е???????????????????
????Resource2.java
????packagecom.zj.lock;
????importjava.util.concurrent.TimeUnit;
????publicclassResource2 {
????privateObjectsyncObject1=newObject();
????privateObjectsyncObject2=newObject();
????publicvoidf() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in f()");
????synchronized(this) {
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in f()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????}
????publicvoidg() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in g()");
????synchronized(syncObject1) {
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in g()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????}
????publicvoidh() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in h()");
????synchronized(syncObject2) {
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in h()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????}
????publicstaticvoidmain(String[] args) {
????finalResource2 rs =newResource2();
????newThread() {
????publicvoidrun() {
????rs.f();
????}
????}.start();
????newThread() {
????publicvoidrun() {
????rs.g();
????}
????}.start();
????rs.h();
????}
????}
?????????
????Thread-0:not synchronized in f()
????Thread-0:synchronized in f()
????main:not synchronized in h()
????main:synchronized in h()
????Thread-1:not synchronized in g()
????Thread-1:synchronized in g()
????Thread-0:synchronized in f()
????main:synchronized in h()
????Thread-1:synchronized in g()
????Thread-0:synchronized in f()
????main:synchronized in h()
????Thread-1:synchronized in g()
????Thread-0:synchronized in f()
????main:synchronized in h()
????Thread-1:synchronized in g()
????Thread-0:synchronized in f()
????main:synchronized in h()
????Thread-1:synchronized in g()
????3.Lock??????
???????????synchronized???????????Lock?????????????????Resource3.java?????Ч???Resource1.java??Resource4.java?????Ч???Resource2.java??
????Resource3.java
????packagecom.zj.lock;
????importjava.util.concurrent.TimeUnit;
????importjava.util.concurrent.locks.Lock;
????importjava.util.concurrent.locks.ReentrantLock;
????publicclassResource3 {
????privateLocklock=newReentrantLock();
????publicvoidf() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in f()");
????lock.lock();
????try{
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in f()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}finally{
????lock.unlock();
????}
????}
????publicvoidg() {
????// other operations should not be locked...
????System.out.println(Thread.currentThread().getName()
????+":not synchronized in g()");
????lock.lock();
????try{
????for(inti = 0; i < 5; i++) {
????System.out.println(Thread.currentThread().getName()
????+":synchronized in g()");
????try{
????TimeUnit.SECONDS.sleep(3);
????}catch(InterruptedException e) {
????e.printStackTrace();
????}
????}
????}finally{
????lock.unlock();
????}
????}