1,2是对象锁,3是类锁
public class SynchronizeDemo1 { static String syn = new String(); static class SynClass { public void myRun() { try { System.out.println(Thread.currentThread().getName() + "进来了"); synchronized (syn) { Thread.sleep(3000); } System.out.println(Thread.currentThread().getName() + "出来了"); } catch (InterruptedException e) { e.printStackTrace(); } } } static class Runnable1 implements Runnable { SynClass synClass; public Runnable1(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun(); } } static class Runnable2 implements Runnable { SynClass synClass; public Runnable2(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun(); } } public static void main(String[] args) { SynClass synClass = new SynClass(); Runnable1 runnable1 = new Runnable1(synClass); Runnable2 runnable2 = new Runnable2(synClass); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } }
运行的结果如下:
等thread1把代码块的执行完,释放了syn的锁,thread2才开始执行。
public class SynchronizeDemo2 { static String syn = new String(); static class SynClass { public void myRun() { try { System.out.println(Thread.currentThread().getName() + "-myRun"); synchronized (this) { Thread.sleep(3000); } System.out.println(Thread.currentThread().getName() + "-myRun"); } catch (InterruptedException e) { e.printStackTrace(); } } public void myRun2() { try { System.out.println(Thread.currentThread().getName() + "-myRun2"); synchronized (this) { Thread.sleep(3000); } System.out.println(Thread.currentThread().getName() + "-myRun2"); } catch (InterruptedException e) { e.printStackTrace(); } } } static class Runnable1 implements Runnable { SynClass synClass; public Runnable1(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun(); } } static class Runnable2 implements Runnable { SynClass synClass; public Runnable2(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun2(); } } public static void main(String[] args) { SynClass synClass = new SynClass(); Runnable1 runnable1 = new Runnable1(synClass); Runnable2 runnable2 = new Runnable2(synClass); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } }
运行的结果如下:
等thread1把代码块的执行完,释放了this的锁,thread2才开始执行。
public class SynchronizeDemo3 extends Thread { @Override public void run() { sync(); } synchronized public void sync(){ System.out.println(Thread.currentThread().getName() + "进来了"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "出来了"); } public static void main(String[] args) { SynchronizeDemo3 synchronizeDemo1 = new SynchronizeDemo3(); Thread thread1 = new Thread(synchronizeDemo1); thread1.setName("thread1"); Thread thread2 = new Thread(synchronizeDemo1); thread2.setName("thread2"); thread1.start(); thread2.start(); } }
运行的结果如下:
等thread1把方法执行完,释放了的锁,thread2才开始执行。
public class SynchronizeDemo4 { static class Runnable1 implements Runnable { @Override public void run() { SynClass.myRun(); } } static class Runnable2 implements Runnable { @Override public void run() { SynClass.myRun2(); } } public static void main(String[] args) { Runnable1 runnable1 = new Runnable1(); Runnable2 runnable2 = new Runnable2(); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } } class SynClass { public synchronized static void myRun() { System.out.println(Thread.currentThread().getName() + "-myRun"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun"); } public synchronized static void myRun2() { System.out.println(Thread.currentThread().getName() + "-myRun2"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun2"); } }
运行的结果如下:
thread1等待thread2执行完才执行,说明是类锁
public class SynchronizeDemo5 { static class Runnable1 implements Runnable { @Override public void run() { synchronized (SynClass2.class){ System.out.println(Thread.currentThread().getName() + "-myRun"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun"); } } } static class Runnable2 implements Runnable { @Override public void run() { synchronized (SynClass2.class){ System.out.println(Thread.currentThread().getName() + "-myRun2"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun2"); } } } public static void main(String[] args) { Runnable1 runnable1 = new Runnable1(); Runnable2 runnable2 = new Runnable2(); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } } class SynClass2 { }
运行结果如下: