更新時(shí)間:2022-06-15 10:36:22 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1526次
Java中的同步是控制多個(gè)線程對(duì)任何共享資源的訪問的能力。
在我們希望只允許一個(gè)線程訪問共享資源的情況下,Java 同步是更好的選擇。
同步主要用于
防止線程干擾。
防止一致性問題。
有兩種類型的同步
進(jìn)程同步
線程同步
在這里,我們將只討論線程同步。
線程同步有互斥和線程間通信兩種。
1.互斥
同步方法。
同步塊。
靜態(tài)同步。
2.協(xié)作(java中的線程間通信)
互斥有助于防止線程在共享數(shù)據(jù)時(shí)相互干擾。可以通過以下三種方式實(shí)現(xiàn):
1.通過使用同步方法
2.通過使用同步塊
3.通過使用靜態(tài)同步
同步是圍繞稱為鎖或監(jiān)視器的內(nèi)部實(shí)體構(gòu)建的。每個(gè)對(duì)象都有一個(gè)與之關(guān)聯(lián)的鎖。按照慣例,需要對(duì)對(duì)象字段進(jìn)行一致訪問的線程必須在訪問對(duì)象之前獲取對(duì)象的鎖,然后在完成訪問時(shí)釋放鎖。
從 Java 5 開始,包 java.util.concurrent.locks 包含多個(gè)鎖實(shí)現(xiàn)。
在沒有同步的情況下理解問題
在這個(gè)例子中,沒有同步,所以輸出不一致。讓我們看看這個(gè)例子:
TestSynchronization1.java
class Table{
void printTable(int n){//method not synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
輸出:
5
100
10
200
15
300
20
400
25
500
如果您將任何方法聲明為同步,則稱為同步方法。
同步方法用于為任何共享資源鎖定對(duì)象。
當(dāng)線程調(diào)用同步方法時(shí),它會(huì)自動(dòng)獲取該對(duì)象的鎖,并在線程完成其任務(wù)時(shí)釋放它。
TestSynchronization2.java
//example of java synchronized method
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
public class TestSynchronization2{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
輸出:
5
10
15
20
25
100
200
300
400
500
在這個(gè)程序中,我們使用匿名類創(chuàng)建了兩個(gè)線程,因此需要較少的編碼。
TestSynchronization3.java
//Program of synchronized method by using annonymous class
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
public class TestSynchronization3{
public static void main(String args[]){
final Table obj = new Table();//only one object
Thread t1=new Thread(){
public void run(){
obj.printTable(5);
}
};
Thread t2=new Thread(){
public void run(){
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}
輸出:
5
10
15
20
25
100
200
300
400
500
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)