摘要:的应用方式代码块作用范围在中,作用对象是调用这个代码块的对象。方法进来了出来了运行的结果如下等把方法执行完,释放了的锁,才开始执行。静态方法运行的结果如下等待执行完才执行,说明是类锁类所的另外一种形式运行结果如下
synchronized的应用方式
代码块:作用范围在{}中,作用对象是调用这个代码块的对象。
方法:作用范围是一个方法,作用对象是调用这个方法的对象。
静态方法:作用范围是这个静态方法,作用对象是这个类的所有对象。
1,2是对象锁,3是类锁
举例 代码块 无thispublic 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才开始执行。
有thispublic 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 { }
运行结果如下:
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/75140.html
摘要:线程安全问题在并发编程学习之基础概念提到,多线程的劣势之一,有个线程安全问题,现在看看下面的例子。那么,该怎么解决呢,很简单,在方法前加个同步锁。运行结果如下有两种情况,是因为看谁先抢占锁,但是输出的算法结果是正确的。 线程安全问题 在java并发编程学习之基础概念提到,多线程的劣势之一,有个线程安全问题,现在看看下面的例子。 public class NotSafeDemo { ...
摘要:不释放持有的锁,释放锁。在调用方法前,必须持有锁,调用唤醒,也要持有锁。休眠一定时间后,进入就绪状态。这两个都能被方法中断当前状态。用法方获取锁判断条件,不满足继续满足执行其他业务方获取锁改变条件通知为什么是而不是会一直循环,直到条件满足。 sleep和wait sleep是Thread类的方法,wait是Object的方法。 sleep可以到处使用,wait必须是在同步方法或者代码...
摘要:线程把的值放在中。线程执行,得到的为。,,独占锁,会导致其他所有需要锁的线程挂起,等待持有锁的线程释放锁。可见性,不保证原子性。乐观锁,不适用锁的情况下实现多线程的变量同步。性能问题在并发量较高的情况下,如果一直不成功,会一直增加的开销。 cas原理 cas全称Compare and swap,比较和交换的意思。原子操作,需要硬件的支持。三个基本操作数:内存地址V,旧的预期值A,要修改...
摘要:显示锁和内置锁内置锁优势代码简洁不会因为没释放锁,导致锁泄露。显示锁优势灵活性强,锁的获取可以被中断,可以尝试获取锁。接口接口主要方法如下获取锁尝试获取锁,表示未加锁的情况。会进行抢锁操作,如果获取不到锁,也会进入阻塞队列等到唤醒。 显示锁和内置锁 内置锁(Synchronized)优势 代码简洁 不会因为没释放锁,导致锁泄露。 显示锁(Lock)优势 灵活性强,锁的获取可以被中...
摘要:和是配套使用的,方法容易导致死锁。方法不会保证线程的资源正常释放方法给线程打个停止标记,将线程的中断状态设置为,并没有马上强制中断线程,线程是否中断由线程自己决定。终结状态,还是返回。方法判断当前线程是否中断,清除中断标志。 resume、suspend、stop resume和suspend是配套使用的,suspend方法容易导致死锁。 stop方法不会保证线程的资源正常释放 i...
阅读 2703·2021-11-25 09:43
阅读 2085·2021-11-24 09:39
阅读 1954·2021-11-17 09:33
阅读 2750·2021-09-27 14:11
阅读 1840·2019-08-30 15:54
阅读 3224·2019-08-26 18:27
阅读 1264·2019-08-23 18:00
阅读 1810·2019-08-23 17:53