synchronized细节问题
生活随笔
收集整理的這篇文章主要介紹了
synchronized细节问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?鎖重入:
/*** synchronized的重入*/ public class SyncDubbo1 {public synchronized void method1(){System.out.println("method1..");method2();}public synchronized void method2(){System.out.println("method2..");method3();}public synchronized void method3(){System.out.println("method3..");}public static void main(String[] args) {final SyncDubbo1 sd = new SyncDubbo1();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {sd.method1();}});t1.start();} }?
/*** synchronized異常*/ public class SyncException {private int i = 0;public synchronized void operation(){while(true){try {i++;Thread.sleep(100);System.out.println(Thread.currentThread().getName() + " , i = " + i);if(i == 10){Integer.parseInt("a");//throw new RuntimeException();}} catch (Exception e) {e.printStackTrace();System.out.println(" log info i = " + i);}}}public static void main(String[] args) {final SyncException se = new SyncException();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {se.operation();}},"t1");t1.start();}} /*** synchronized異常*/ public class SyncException {private int i = 0;public synchronized void operation(){while(true){try {i++;Thread.sleep(100);System.out.println(Thread.currentThread().getName() + " , i = " + i);if(i == 10){Integer.parseInt("a");//throw new RuntimeException();}} catch (InterruptedException e) {e.printStackTrace();System.out.println(" log info i = " + i);throw new RuntimeException();//continue;}}}public static void main(String[] args) {final SyncException se = new SyncException();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {se.operation();}},"t1");t1.start();}}?
/*** 使用synchronized代碼塊加鎖,比較靈活*/ public class ObjectLock {public void method1(){synchronized (this) { //對象鎖try {System.out.println("do method1..");Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}}public void method2(){ //類鎖synchronized (ObjectLock.class) {try {System.out.println("do method2..");Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}}private Object lock = new Object();public void method3(){ //任何對象鎖synchronized (lock) {try {System.out.println("do method3..");Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {final ObjectLock objLock = new ObjectLock();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {objLock.method1();}});Thread t2 = new Thread(new Runnable() {@Overridepublic void run() {objLock.method2();}});Thread t3 = new Thread(new Runnable() {@Overridepublic void run() {objLock.method3();}});t1.start();t2.start();t3.start();}} /*** synchronized代碼塊對字符串的鎖,注意String常量池的緩存功能*/ public class StringLock {public void method() {//new String("字符串常量")synchronized (new String("字符串常量")) {try {while(true){System.out.println("當前線程 : " + Thread.currentThread().getName() + "開始");Thread.sleep(1000); System.out.println("當前線程 : " + Thread.currentThread().getName() + "結束");}} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {final StringLock stringLock = new StringLock();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {stringLock.method();}},"t1");Thread t2 = new Thread(new Runnable() {@Overridepublic void run() {stringLock.method();}},"t2");t1.start();t2.start();} } /*** 鎖對象的改變問題*/ public class ChangeLock {private String lock = "lock";private void method(){synchronized (lock) {try {System.out.println("當前線程 : " + Thread.currentThread().getName() + "開始");lock = "change lock";Thread.sleep(2000);System.out.println("當前線程 : " + Thread.currentThread().getName() + "結束");} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {final ChangeLock changeLock = new ChangeLock();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {changeLock.method();}},"t1");Thread t2 = new Thread(new Runnable() {@Overridepublic void run() {changeLock.method();}},"t2");t1.start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}t2.start();}} /*** 同一對象屬性的修改不會影響鎖的情況*/ public class ModifyLock {private String name ;private int age ;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public synchronized void changeAttributte(String name, int age) {try {System.out.println("當前線程 : " + Thread.currentThread().getName() + " 開始");this.setName(name);this.setAge(age);System.out.println("當前線程 : " + Thread.currentThread().getName() + " 修改對象內容為: " + this.getName() + ", " + this.getAge());Thread.sleep(2000);System.out.println("當前線程 : " + Thread.currentThread().getName() + " 結束");} catch (InterruptedException e) {e.printStackTrace();}}public static void main(String[] args) {final ModifyLock modifyLock = new ModifyLock();Thread t1 = new Thread(new Runnable() {@Overridepublic void run() {modifyLock.changeAttributte("張三", 20);}},"t1");Thread t2 = new Thread(new Runnable() {@Overridepublic void run() {modifyLock.changeAttributte("李四", 21);}},"t2");t1.start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}t2.start();}}?
?
總結
以上是生活随笔為你收集整理的synchronized细节问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ActiveMQ 消息持久化
- 下一篇: queue模拟