使用synchronized代碼塊及其原理?java
使用synchronized方法?this
分析靜態方法所使用的同步監視器對象是什麼?spa
/** * @Title: TraditionalThreadSynchronized.java * @Package com.lh.threadtest * @Description: TODO * @author Liu * @date 2018年1月15日 下午6:38:24 * @version V1.0 */ package com.lh.threadtest.t3; import java.util.concurrent.TimeUnit; /** * @ClassName: TraditionalThreadSynchronized * @Description: 傳統線程互斥技術 * @author Liu * @date 2018年1月15日 下午6:38:24 * */ public class TraditionalThreadSynchronized { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { new TraditionalThreadSynchronized().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("zhangsan"); } } }).start(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("lisi"); } } }).start(); } class Outputer{ public void output(String name){ String xxx = "xxx"; //不行,能夠認爲是不一樣的鑰匙,不能起到互斥的效果 // synchronized (name) { // for(int i = 0; i< name.length(); i++){ // System.out.print(name.charAt(i)); // } // System.out.println(); // } // synchronized (this) { synchronized (xxx) { for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } } }
/** * @Title: TraditionalThreadSynchronized.java * @Package com.lh.threadtest * @Description: TODO * @author Liu * @date 2018年1月15日 下午6:38:24 * @version V1.0 */ package com.lh.threadtest.t3; import java.util.concurrent.TimeUnit; /** * @ClassName: TraditionalThreadSynchronized * @Description: 傳統線程互斥技術 * @author Liu * @date 2018年1月15日 下午6:38:24 * */ public class TraditionalThreadSynchronized2 { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { new TraditionalThreadSynchronized2().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("zhangsan"); } } }).start(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } // outputer.output("lisi"); //改爲下面這種方式也是不行的(外部調用的對象不是同一個)!!! new Outputer().output("lisi"); } } }).start(); } class Outputer{ public void output(String name){ String xxx = "xxx"; //不行,能夠認爲是不一樣的鑰匙,不能起到互斥的效果 // synchronized (name) { // for(int i = 0; i< name.length(); i++){ // System.out.print(name.charAt(i)); // } // System.out.println(); // } // synchronized (this) { synchronized (xxx) { for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } } }
/** * @Title: TraditionalThreadSynchronized.java * @Package com.lh.threadtest * @Description: TODO * @author Liu * @date 2018年1月15日 下午6:38:24 * @version V1.0 */ package com.lh.threadtest.t3; import java.util.concurrent.TimeUnit; /** * @ClassName: TraditionalThreadSynchronized * @Description: 傳統線程互斥技術 * @author Liu * @date 2018年1月15日 下午6:38:24 * */ public class TraditionalThreadSynchronized3 { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { new TraditionalThreadSynchronized3().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("zhangsan"); } } }).start(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("lisi"); } } }).start(); } class Outputer{ //方法名前加synchronized關鍵字!對整個方法體加鎖! public synchronized void output(String name){ //線程間是互斥的!! for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }
/** * @Title: TraditionalThreadSynchronized.java * @Package com.lh.threadtest * @Description: TODO * @author Liu * @date 2018年1月15日 下午6:38:24 * @version V1.0 */ package com.lh.threadtest.t3; import java.util.concurrent.TimeUnit; /** * @ClassName: TraditionalThreadSynchronized * @Description: 傳統線程互斥技術 (對象鎖必須保證惟一性,即保證線程互斥性) * @author Liu * @date 2018年1月15日 下午6:38:24 * */ public class TraditionalThreadSynchronized4 { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { new TraditionalThreadSynchronized4().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("zhangsan"); } } }).start(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output2("lisi"); } } }).start(); } class Outputer{ //方法名前加synchronized關鍵字!對整個方法體加鎖! public void output(String name){ synchronized (this) { for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } //方法名前的synchronized使用的鎖默認是當前對象! public synchronized void output2(String name){ //線程間是互斥的!! for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }
/** * @Title: TraditionalThreadSynchronized.java * @Package com.lh.threadtest * @Description: TODO * @author Liu * @date 2018年1月15日 下午6:38:24 * @version V1.0 */ package com.lh.threadtest.t3; import java.util.concurrent.TimeUnit; /** * @ClassName: TraditionalThreadSynchronized * @Description: 傳統線程互斥技術 (對象鎖必須保證惟一性,即保證線程互斥性) * @author Liu * @date 2018年1月15日 下午6:38:24 * */ public class TraditionalThreadSynchronized5 { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { new TraditionalThreadSynchronized5().init(); } private void init(){ final Outputer outputer = new Outputer(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output("zhangsan"); } } }).start(); new Thread(new Runnable() { public void run() { while(true){ try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } outputer.output3("lisi"); } } }).start(); } static class Outputer{ //方法名前加synchronized關鍵字!對整個方法體加鎖! public void output(String name){ synchronized (Outputer.class) { for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } //方法名前的synchronized使用的鎖默認是當前對象! public synchronized void output2(String name){ //線程間是互斥的!! for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } //靜態方法名前的synchronized使用的是class對象! public static synchronized void output3(String name){ //線程間是互斥的!! for(int i = 0; i< name.length(); i++){ System.out.print(name.charAt(i)); } System.out.println(); } } }
一、內部類的做用:能夠訪問外部類的成員屬性線程