内容简介:1,2是对象锁,3是类锁运行的结果如下:
synchronized的应用方式
- 代码块:作用范围在{}中,作用对象是调用这个代码块的对象。
- 方法:作用范围是一个方法,作用对象是调用这个方法的对象。
- 静态方法:作用范围是这个静态方法,作用对象是这个类的所有对象。
1,2是对象锁,3是类锁
举例
代码块
无this
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才开始执行。
有this
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 { }
运行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Go并发编程-并发编程难在哪里
- Java并发编程的艺术,解读并发编程的优缺点
- Java并发系列—并发编程基础
- JAVA并发编程之并发模拟工具
- Java并发系列—并发编程的挑战
- c++并发编程—分布式编程
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
TensorFlow:实战Google深度学习框架(第2版)
顾思宇、梁博文、郑泽宇 / 电子工业出版社 / 2018-2-1 / 89
TensorFlow是谷歌2015年开源的主流深度学习框架,目前已得到广泛应用。《TensorFlow:实战Google深度学习框架(第2版)》为TensorFlow入门参考书,旨在帮助读者以快速、有效的方式上手TensorFlow和深度学习。书中省略了烦琐的数学模型推导,从实际应用问题出发,通过具体的TensorFlow示例介绍如何使用深度学习解决实际问题。书中包含深度学习的入门知识和大量实践经......一起来看看 《TensorFlow:实战Google深度学习框架(第2版)》 这本书的介绍吧!