1synchronized原理
在java中每一個對象有且僅有一個同步鎖,這也意味着同步鎖是依賴對象而存在。
當我們在調用對象的synchronized方法時,就獲取了該對象的同步鎖,例如synchronized(obj)就獲取了"obj這個對象"的同步鎖
不同線程對同步鎖的訪問是互斥的,也就是說,在某個時間點,對象的同步鎖只能被一個線程獲取到!通過同步鎖,我們可以實現對對象/方法的互斥訪問。
例如現在有兩個線程A B他們都會訪問對象的同步鎖,在某一時刻,線程A獲得對象的同步鎖,並進行一些操作,而此時線程B也企圖獲取對象的同步鎖,線程B這個操作就會失敗,他必須等待,等到線程A釋放了這個對象的同步鎖,他纔可以執行。
2synchronized基本規則
synchronized的規則基本有下面三條
第一條:當一個線程訪問某個對象的synchronized方法或者代碼塊,其他線程對該對象的訪問將會被阻塞。
第二條:當一個線程訪問 某對象的synchronized方法或者代碼塊時候,其他線程仍然可以訪問該獨享的非同步代碼塊。
第三條:當一個線程訪問某個對象的synchronized方法或者代碼塊時候,其他線程對該對象的其他的synchronized方法或者代碼塊的訪問將被阻塞
第一條 代碼演示模塊
package com.tuhu.filt.javadata;
public class MyRunable implements Runnable {
@Override
public void run() {
synchronized (this){
for (int i = 0;i < 5;i++){
try {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+"loop"
);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Demo_1{
public static void main(String[] args) {
Runnable demo = new MyRunable();
Thread t1 = new Thread(demo,"t1");
Thread t2 = new Thread(demo,"t2");
t1.start();
t2.start();
}
}
run()方法中存在"synchronized(this)"代碼塊,而且t1和t2都是基於"demo這個Runnable"對象創建的線程,這就意味着,我們可以將synchronized(this)中的this看作是"demo這個Runnable對象";因此,線程1和線程2共享demo對象的同步鎖,所以當一個線程運行的時候,另外一個線程必須等待"運行線程"釋放"demo"的同步鎖之後才能運行
下面我們將代碼稍微修改一下,再來看看運行結果是怎麼樣的
package com.tuhu.filt.javadata;
public class MyRunable implements Runnable {
@Override
public void run() {
synchronized (this){
for (int i = 0;i < 5;i++){
try {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+"loop"
);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Demo_1{
public static void main(String[] args) {
Runnable demo = new MyRunable();
Thread t1 = new Thread(demo,"t1");
Thread t2 = new Thread(demo,"t2");
t1.start();
t2.start();
}
}
class MyThread2 extends Thread{
public MyThread2(String name){
super(name);
}
@Override
public void run(){
synchronized (this){
for(int i = 0;i<5;i++){
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()
+"loop");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Demo_2{
public static void main(String[] args) {
Thread t1 = new MyThread2("t1");
Thread t2 = new MyThread2("t2");
t1.start();
t2.start();
}
}
比較Demo1和Demo2 我們發現 Demo2中的MyThread類是直接繼承於Thread,而且t1和t2都是直接繼承於MyThread子線程,幸運的是 在Demo2中也調用了synchronized(this),正如Demo1中的run方法,也調用了synchronized(this)一樣,但是正如我們看到的結果一樣這裏的接過不是我們預期的那種將t2阻塞住,這裏的synchronized(this)代表的是MyThread對象,而t1和t2是兩個不同的MyThread對象,因此t1和t2在執行synchronized(this)時,獲取的是不同對象的同步鎖,對於Demo1而言,synchronized(this)中的this代表的是MyRunable對象;t1和t2共同一個MyRunable對象,因此一個對象獲得了同步鎖,會造成另一個線程的等待。
第二條的代碼演示(當一個線程訪問該對象的synchronized方法或者代碼塊的時候,其他線程仍然可以訪問該對象的非同步代碼塊)
下面是synchronized代碼塊的演示程序
package com.tuhu.filt.javadata;
class Count{
/**
* synMethod()是一個同步塊方法
*/
public void synMethod(){
synchronized (this){
try {
for (int i =0;i<5;i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()
+"synMethod loop"+i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 非同步方法nonSynMethod()
*/
public void nonSynMethod(){
try {
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+"synMethod loop"+i
);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Demo2 {
public static void main(String[] args) {
final Count count = new Count();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethod();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
count.nonSynMethod();;
}
},"t2");
t1.start();
t2.start();
}
}
結果說明
主線程中新建兩個子線程t1 和 t2 t1會調用count對象的synMethod()方法,該方法內含有同步塊;而t2則會調用,count對象的nonSynMethod()方法,該方法不是同步方法。t1運行時,雖然調用的synchronized(this)獲取"count的同步鎖";但是並沒有造成t2的阻塞,因爲t2沒有用到"count"同步鎖
第三條
當一個線程訪問某對象的synchronized方法或者synchronized代碼塊時,其他線程對該對象的其他synchronized方法將會被阻塞,只需要將上面的nonSynMethod()方法體也用synchronized(this)修飾。
代碼如下
package com.tuhu.filt.javadata;
class Count{
/**
* synMethod()是一個同步塊方法
*/
public void synMethod(){
synchronized (this){
try {
for (int i =0;i<5;i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()
+"synMethod loop"+i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 非同步方法nonSynMethod()
*/
public void nonSynMethod(){
synchronized (this){
try {
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+"synMethod loop"+i
);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Demo2 {
public static void main(String[] args) {
final Count count = new Count();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethod();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
count.nonSynMethod();;
}
},"t2");
t1.start();
t2.start();
}
}
結果說明
主線程中 新建了兩個子線程t1和t2 t1 和 t2運行時都調用,synchronized(this),這個this是Count對象(Count),而t1和t2共用count. 因此在t1運行時 t2會被阻塞,t1釋放同步鎖之後,t2才能運行
3 synchronized方法和synchronized代碼塊
synchronized方法是用synchronized修飾方法,而synchronized代碼塊則是用synchronized修飾代碼塊
synchronized方法示例
public synchronized void foo1(){
System.out.println("synchronized method");
}
synchronized代碼塊
public void foo2(){
synchronized (this){
System.out.println("synchronized method");
}
}
synchronized代碼塊中this指的是當前對象,也可以將this替換成obj,則foo2()在執行synchronized(obj)時就獲取的是obj的同步鎖
synchronized代碼塊可以更精確的控制衝突限制訪問區域,有時候表現的更效率,下面通過一個示例來演示。
下面上代碼證明一下
package com.tuhu.filt.javadata;
public class Demo4 {
public synchronized void synMethod(){
for(int i = 0;i<1000000;i++){
;
}
}
public void synBlock(){
synchronized (this){
for(int i =0;i<1000000;i++){
;
}
}
}
public static void main(String[] args) {
Demo4 demo = new Demo4();
long start,diff;
start = System.currentTimeMillis();
demo.synMethod();
diff = System.currentTimeMillis() - start;
System.out.println("synMethod()運行的時間:" + diff);
start = System.currentTimeMillis();
demo.synBlock();
diff = System.currentTimeMillis() - start;
System.out.println("synBlock()運行的時間:" +diff);
}
}
(某一次)執行結果
synMethod()運行的時間:5
synBlock()運行的時間:3
4 實例鎖 和 全局鎖
實例鎖:鎖在某一個實例對象上,如果該類是單例,那麼該鎖也具有全局鎖的概念,實例鎖對應的就是synchronized關鍵字。
全局鎖:該鎖針對的是類,無論實例多少個對象,那麼線程都共享該鎖
全局鎖對應的就是static synchronized(或者是鎖在該類的class或者classloader對象上)
下面舉一個關於 "實例鎖"和"全局鎖"有一個很形象的例子:
public class Something {
public synchronized void isSyncA(){}
public synchronized void isSyncB(){}
public static synchronized void cSyncA(){}
public static synchronized void cSyncB(){}
}
假設,Something有兩個實例x和y。分析下面4組表達式獲取鎖的情況
(01) x.isSyncA()與x.isSyncB()
(02) x.isSyncA()與y.isSyncA()
(03) x.cSyncA()與y.cSyncB()
(04) x.isSyncA()與Something.cSyncA()
01 這個在上面synchronized規則中已經講過了肯定是不能同時使用的,因爲他們都是訪問同一個對象(對象x)的同步鎖!
package com.tuhu.filt.javadata;
public class Something {
public synchronized void isSyncA(){
try{
for(int i = 0;i<5;i++){
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+":isSyncA"
);
}
}catch (InterruptedException ie){
}
}
public synchronized void isSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":isSyncB"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncA(){}
public static synchronized void cSyncB(){}
}
class LockTest1{
Something x = new Something();
Something y = new Something();
private void test1(){
Thread tl1 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncA();
}
}
,"tl1" );
Thread tl2 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncB();
}
}
,"tl2");
tl1.start();
tl2.start();
}
public static void main(String[] args) {
LockTest1 demo = new LockTest1();
demo.test1();
}
}
02 可以同時被訪問,因爲訪問的不是同一個對象的同步鎖,x.isSyncA()訪問的是x的同步鎖,而y.isSyncA()訪問的是y的同步鎖
下面上代碼
package com.tuhu.filt.javadata;
public class Something {
public synchronized void isSyncA(){
try{
for(int i = 0;i<5;i++){
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+":isSyncA"
);
}
}catch (InterruptedException ie){
}
}
public synchronized void isSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":isSyncB"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncA(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":cSyncA"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":cSyncB"
);
}
}catch (InterruptedException ie){
}
}
}
class LockTest1{
Something x = new Something();
Something y = new Something();
/**
* 測01的兩個,如果一個線程A訪問一個對象的synchronized方法
* 一個線程B去訪問同一個對象的synchronized方法就會被阻塞住
* @param args
*/
// private void test1(){
// Thread tl1 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncA();
// }
// }
// ,"tl1" );
//
// Thread tl2 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncB();
// }
// }
// ,"tl2");
// tl1.start();
// tl2.start();
// }
/**
* 兩個線程各自訪問不同對象的synchronized方法
* 是暢通無阻的
*/
private void test2(){
Thread t21 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncA();
}
}
,"t21");
Thread t22 = new Thread(
new Runnable() {
@Override
public void run() {
y.isSyncA();
}
}
,"t22");
t21.start();
t22.start();
}
public static void main(String[] args) {
LockTest1 demo = new LockTest1();
demo.test2();
}
}
03 不能被同時訪問 因爲cSyncA() 和 cSyncB() 都是static 類型的,x.cSynA() 相當於Something.isSynA(),y.cSyncB() 因此他們共用一個同步鎖,不能被同時訪問
下面上代碼
package com.tuhu.filt.javadata;
public class Something {
public synchronized void isSyncA(){
try{
for(int i = 0;i<5;i++){
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+":isSyncA"
);
}
}catch (InterruptedException ie){
}
}
public synchronized void isSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":isSyncB"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncA(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":cSyncA"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":cSyncB"
);
}
}catch (InterruptedException ie){
}
}
}
class LockTest1{
Something x = new Something();
Something y = new Something();
/**
* 測01的兩個,如果一個線程A訪問一個對象的synchronized方法
* 一個線程B去訪問同一個對象的synchronized方法就會被阻塞住
* @param args
*/
// private void test1(){
// Thread tl1 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncA();
// }
// }
// ,"tl1" );
//
// Thread tl2 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncB();
// }
// }
// ,"tl2");
// tl1.start();
// tl2.start();
// }
/**
* 兩個線程各自訪問不同對象的synchronized方法
* 是暢通無阻的
*/
// private void test2(){
// Thread t21 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncA();
// }
// }
// ,"t21");
//
// Thread t22 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// y.isSyncA();
// }
// }
// ,"t22");
// t21.start();
// t22.start();
//
// }
private void test3(){
Thread t31 = new Thread(
new Runnable() {
@Override
public void run() {
Something.cSyncA();
}
}
,"t31");
Thread t32 = new Thread(
new Runnable() {
@Override
public void run() {
Something.cSyncB();
}
}
,"t32");
t31.start();
t32.start();
}
public static void main(String[] args) {
LockTest1 demo = new LockTest1();
demo.test3();
}
}
04 可以被同時訪問 因爲isSyncA()是實例方法,x.isSyncA()使用的是對象x的鎖;而cSyncA()是靜態方法,Something.cSyncA()可以理解對使用的是"類的鎖"。 因此他們是可以被同時訪問的
下面上代碼
package com.tuhu.filt.javadata;
public class Something {
public synchronized void isSyncA(){
try{
for(int i = 0;i<5;i++){
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+":isSyncA"
);
}
}catch (InterruptedException ie){
}
}
public synchronized void isSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":isSyncB"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncA(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":cSyncA"
);
}
}catch (InterruptedException ie){
}
}
public static synchronized void cSyncB(){
try{
for(int i = 0;i<5;i++) {
Thread.sleep(100);
System.out.println(
Thread.currentThread().getName()
+ ":cSyncB"
);
}
}catch (InterruptedException ie){
}
}
}
class LockTest1{
Something x = new Something();
Something y = new Something();
/**
* 測01的兩個,如果一個線程A訪問一個對象的synchronized方法
* 一個線程B去訪問同一個對象的synchronized方法就會被阻塞住
* @param args
*/
// private void test1(){
// Thread tl1 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncA();
// }
// }
// ,"tl1" );
//
// Thread tl2 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncB();
// }
// }
// ,"tl2");
// tl1.start();
// tl2.start();
// }
/**
* 兩個線程各自訪問不同對象的synchronized方法
* 是暢通無阻的
*/
// private void test2(){
// Thread t21 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// x.isSyncA();
// }
// }
// ,"t21");
//
// Thread t22 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// y.isSyncA();
// }
// }
// ,"t22");
// t21.start();
// t22.start();
//
// }
/**
* 都是如遇類的靜態方法
* 我們可以把它理解爲類的鎖
* 他們都是類的鎖,所以同時訪問會產生阻塞
*/
// private void test3(){
// Thread t31 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// Something.cSyncA();
// }
// }
// ,"t31");
//
//
// Thread t32 = new Thread(
// new Runnable() {
// @Override
// public void run() {
// Something.cSyncB();
// }
// }
// ,"t32");
//
// t31.start();
// t32.start();
// }
private void test4(){
Thread t41 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncA();
}
}
);
Thread t42 = new Thread(
new Runnable() {
@Override
public void run() {
Something.cSyncA();
}
}
);
t41.start();;
t42.start();
}
public static void main(String[] args) {
LockTest1 demo = new LockTest1();
demo.test4();
}
}
完結 嘿嘿 lisiming敲於20180804 星期六 17:57 在途虎養車公司