1. 程式人生 > >Java多執行緒11:多執行緒同步操作

Java多執行緒11:多執行緒同步操作

什麼情況下需要同步

1、當多執行緒併發,有多段程式碼同時執行時,有時希望某一段程式碼執行的過程中CPU不要切換到其他執行緒工作。這時就需要執行緒同步。 2、如果兩段程式碼是同步的,那麼同一段時間只能執行一段時間,在一段程式碼沒執行結束之前,不會執行另外一段程式碼。

同步程式碼塊

1、使用synchronized關鍵字加上一個鎖物件來定義一段程式碼,這就叫同步程式碼塊。 2、多個同步程式碼塊如果使用相同的鎖物件,那麼他們就是同步的。

package Thread;
public class Demo1_Synchronized {
     
     public static void main(String[] args) {
          Printer p = new Printer();
          new Thread(){
              public void run() {
                   for(int i = 0; i < 10000; i++) {
                        p.print1();
                   }
              }
          }.start();
          
          new Thread() {
              public void run() {
                   for(int i =0;i < 10000; i++) {
                        p.print2();
                   }
              }
          }.start();
     }
}
class Printer {
     Demo d = new Demo();
     public void print1() {
          //synchronized(new Demo()){
          synchronized(d){             //同步程式碼塊,鎖機制,鎖物件可以是任意的
              System.out.print("黑");
              System.out.print("馬");
              System.out.print("程");
              System.out.print("序");
              System.out.print("員");
              System.out.print("\r\n");
          }
     }
     
     public void print2() {
          //鎖物件不能用匿名物件,因為匿名物件不是同一個物件
          //synchronized(new Demo()){
          synchronized(d){
              System.out.print("傳");
              System.out.print("智");
              System.out.print("播");
              System.out.print("客");
              System.out.print("\r\n");
          }
          
     }
}
class Dmeo {
     
}
同步方法

1、非靜態的同步方法的鎖物件是this。 2、靜態的同步方法的鎖物件是該類的位元組碼物件。

package Thread;
public class Demo2_Synchronized {
     
     public static void main(String[] args) {
          Printer1 p = new Printer1();
          new Thread(){
              public void run() {
                   for(int i = 0; i < 10000; i++) {
                        p.print1();
                   }
              }
          }.start();
          
          new Thread() {
              public void run() {
                   for(int i =0;i < 10000; i++) {
                        p.print2();
                   }
              }
          }.start();
     }
}
class Printer1 {
     Demo d = new Demo();
     //非靜態的同步方法的鎖物件是什麼?
     //答:非靜態的鎖物件是this
     
     //靜態的同步方法的鎖物件是什麼?
     //該類的位元組碼物件
     //  public synchronized void print1() { 
     public static synchronized void print1() {      //同步方法只需要在方法上加synchronized關鍵字即可
          System.out.print("黑");
          System.out.print("馬");
          System.out.print("程");
          System.out.print("序");
          System.out.print("員");
          System.out.print("\r\n");
          
     }
     
     public static void print2() {
          //鎖物件不能用匿名物件,因為匿名物件不是同一個物件
          //synchronized(new Demo()){
          synchronized(Printer1.class){
              System.out.print("傳");
              System.out.print("智");
              System.out.print("播");
              System.out.print("客");
              System.out.print("\r\n");
          }
          
     }
}
class Demo1 {
     
}