zoukankan      html  css  js  c++  java
  • Java Synchronized的用法

    一.基本方法

    Synchronized用于线程同步与互斥,用法有以下两种:

    synchronized 方法和 synchronized 代码块

    方法1.synchronized 方法:通过在方法声明中加入 synchronized 关键字来声明 synchronized 方法

        public synchronized void methodName(class className);

    方法2.synchronized 代码块:通过 synchronized关键字来声明 synchronized 代码块

        synchronized(this) {
            //允许访问控制的代码
        }

    二.实现线程互斥

    示例代码1 (使用 synchronized 方法):

     1 public class Test {
     2 
     3     public static void main(String[] args) {
     4         new Test().init();
     5     }
     6     
     7     private void init(){
     8         Outputer outputer = new Outputer();
     9         //创建第一个线程
    10         new Thread(new Runnable(){
    11             public void run() {
    12                 while(true){
    13                     try {
    14                         Thread.sleep(10);
    15                     } catch (InterruptedException e) {
    16                         e.printStackTrace();
    17                     }
    18                     outputer.output("shen_smile");
    19                 }
    20             }
    21         }).start();
    22         //创建第二个线程
    23         new Thread(new Runnable(){
    24             public void run() {
    25                 while(true){
    26                     try {
    27                         Thread.sleep(10);
    28                     } catch (InterruptedException e) {
    29                         e.printStackTrace();
    30                     }
    31                     outputer.output("SynchronizedTest");
    32                 }
    33             }
    34         }).start();
    35     }
    36     
    37     class Outputer{
    38         //用synchronized方法实现互斥调用
    39         public synchronized void output(String name){
    40             int len = name.length();
    41             for(int i=0;i<len;i++){
    42                 System.out.print(name.charAt(i));//将name以字母为单位打印
    43             }
    44             System.out.println();
    45         }
    46     }
    47 }

    示例代码2 (使用 synchronized 代码块):

     1 public class Test {
     2 
     3     public static void main(String[] args) {
     4         new Test().init();
     5     }
     6     
     7     private void init(){
     8         Outputer outputer = new Outputer();
     9         //创建第一个线程
    10         new Thread(new Runnable(){
    11             public void run() {
    12                 while(true){
    13                     try {
    14                         Thread.sleep(10);
    15                     } catch (InterruptedException e) {
    16                         e.printStackTrace();
    17                     }
    18                     outputer.output("shen_smile");
    19                 }
    20             }
    21         }).start();
    22         //创建第二个线程
    23         new Thread(new Runnable(){
    24             public void run() {
    25                 while(true){
    26                     try {
    27                         Thread.sleep(10);
    28                     } catch (InterruptedException e) {
    29                         e.printStackTrace();
    30                     }
    31                     outputer.output("SynchronizedTest");
    32                 }
    33             }
    34         }).start();
    35     }
    36     class Outputer{
    37         //用synchronized代码块实现互斥调用
    38         public void output(String name){
    39             int len = name.length();
    40             synchronized(this){
    41                 for(int i=0;i<len;i++){
    42                     System.out.print(name.charAt(i));//将name以字母为单位打印
    43                 }
    44                 System.out.println();
    45             }
    46         }
    47     }
    48 }

    三.实现线程同步

    结合 synchronized方法, synchronized代码块 和 静态synchronized方法:

    1.实现 synchronized方法和 synchronized代码块同步

    这里实现的是两个线程对相同对象的不同方法之间的同步,两个方法分别用 synchronized方法和 synchronized代码块实现,本例中 output方法中的synchronized应该写成方法2的标准样式:

        synchronized(this) {
            //允许访问控制的代码
        }

    示例代码:

     1 public class Test {
     2 
     3     public static void main(String[] args) {
     4         new Test().init();
     5     }
     6     
     7     private void init(){
     8         Outputer outputer = new Outputer();
     9         //创建第一个线程
    10         new Thread(new Runnable(){
    11             public void run() {
    12                 while(true){
    13                     try {
    14                         Thread.sleep(10);
    15                     } catch (InterruptedException e) {
    16                         e.printStackTrace();
    17                     }
    18                     outputer.output("shen_smile");
    19                 }
    20             }
    21         }).start();
    22         //创建第二个线程
    23         new Thread(new Runnable(){
    24             public void run() {
    25                 while(true){
    26                     try {
    27                         Thread.sleep(10);
    28                     } catch (InterruptedException e) {
    29                         e.printStackTrace();
    30                     }
    31                     outputer.output2("SynchronizedTest");
    32                 }
    33             }
    34         }).start();
    35     }
    36     
    37     class Outputer{
    38         //synchronized代码块
    39         public void output(String name){
    40             int len = name.length();
    41             synchronized(this){
    42                 for(int i=0;i<len;i++){
    43                     System.out.print(name.charAt(i));//将name以字母为单位打印
    44                 }
    45                 System.out.println();
    46             }
    47         }
    48         //synchronized方法
    49         public synchronized void output2(String name){
    50             int len = name.length();
    51             for(int i=0;i<len;i++){
    52                 System.out.print(name.charAt(i));
    53             }
    54             System.out.println();
    55         }
    56     }
    57 }
    

    2.实现 静态synchronized方法和 synchronized代码块同步

    这里实现的是两个线程对相同对象的 静态synchronized方法和 synchronized代码块同步,普通 synchronized代码块方法要和 静态synchronized方法同步,就必须在synchronized代码块方法中使用类的字节码,本例中字节码为 Outputer.class

    ,将以下代码

        synchronized(this) {
            //允许访问控制的代码
        }

    改为

        synchronized(Outputer.class) {
            //允许访问控制的代码
        }

    示例代码:

     1 public class Test {
     2 
     3     public static void main(String[] args) {
     4         new Test().init();
     5     }
     6     
     7     private void init(){
     8         Outputer outputer = new Outputer();
     9         //创建第一个线程
    10         new Thread(new Runnable(){
    11             public void run() {
    12                 while(true){
    13                     try {
    14                         Thread.sleep(10);
    15                     } catch (InterruptedException e) {
    16                         e.printStackTrace();
    17                     }
    18                     outputer.output("shen_smile");
    19                 }
    20             }
    21         }).start();
    22         //创建第二个线程
    23         new Thread(new Runnable(){
    24             public void run() {
    25                 while(true){
    26                     try {
    27                         Thread.sleep(10);
    28                     } catch (InterruptedException e) {
    29                         e.printStackTrace();
    30                     }
    31                     outputer.output3("SynchronizedTest");
    32                 }
    33             }
    34         }).start();
    35     }
    36     
    37     static class Outputer{
    38         //synchronized代码块
    39         public void output(String name){
    40             int len = name.length();
    41             synchronized(Outputer.class){
    42                 for(int i=0;i<len;i++){
    43                     System.out.print(name.charAt(i));//将name以字母为单位打印
    44                 }
    45                 System.out.println();
    46             }
    47         }
    48         //静态synchronized方法
    49         public static synchronized void output3(String name){
    50             int len = name.length();
    51             for(int i=0;i<len;i++){
    52                 System.out.print(name.charAt(i));
    53             }
    54             System.out.println();
    55         }
    56     }
    57 }
  • 相关阅读:
    1206 冲刺三
    1130持续更新
    1128项目跟进
    冲刺一1123(总结)
    冲刺一
    1117 新冲刺
    0621 第三次冲刺及课程设计
    0621回顾和总结
    实验四主存空间的分配和回收
    学习进度条
  • 原文地址:https://www.cnblogs.com/shen-smile/p/5136737.html
Copyright © 2011-2022 走看看