Java多线程基础(三)Java传统线程互斥技术

jopen 8年前

Java多线程基础(三)Java传统线程互斥技术

java的线程互斥主要通过synchronized关键字实现。下面的示例代码展示了几种使用synchronized关键字的基本用法。

package cn.king;    public class TraditionalThreadSynchronized {        public static void main(String[] args) {          new TraditionalThreadSynchronized().foo();      }        private void foo() {          // printer须是final的,否则无法编译。这主要是为了保证printer的一致性。          final Printer printer = new Printer();          new Thread(new Runnable() {              @Override              public void run() {                  while(true) {                      try {                          Thread.sleep(10);                      } catch (InterruptedException e) {                          e.printStackTrace();                      }                      /* * Cannot refer to a non-final variable printer * inside an inner class defined in a different method * 更多内容可参阅java8 lambda表达式(闭包)相关知识 */                      printer.output("123456789");                  }              }          }).start();          new Thread(new Runnable() {              @Override              public void run() {                  while(true) {                      try {                          Thread.sleep(10);                      } catch (InterruptedException e) {                          e.printStackTrace();                      }                      printer.output("abcdefghi");                  }              }          }).start();      }        static class Printer {          String _lock = "";          public void output(String name) {              int len = name.length();              // 同步代码块                /* 方法1: * 以this作为锁对象, * 与使用this加锁的代码块或synchronized方法互斥 */              // synchronized(this) {                /* 方法2: * 以Outputer类的字节码对象(该对象由虚拟机自动创建)作为锁对象, * 与使用Outputer.class加锁的代码块或static synchronized方法互斥 */              // synchronized(Outputer.class) {                /* 方法3: * 以自定义对象作为锁对象, * 与使用_lock加锁的代码块互斥 */              synchronized(_lock) {                  for(int i=0; i<len; i++) {                      System.out.print(name.charAt(i));                  }                  System.out.println();              }          }            // 同步方法,相当于synchronized(this){}          public synchronized void output2(String name) {              int len = name.length();              for(int i=0; i<len; i++) {                  System.out.print(name.charAt(i));              }              System.out.println();          }            // 静态同步方法,相当于synchronized(Outputer.class){}          public static synchronized void output3(String name) {              int len = name.length();              for(int i=0; i<len; i++) {                  System.out.print(name.charAt(i));              }              System.out.println();          }      }  }

上面的代码中展示了三种基本的线程互斥实现。下面详述三种方法的实现特点和适用情况。

  1. 以this作为锁对象
    实现方式
    synchronized(this){…}
    synchronized实例方法
    适用情况
    适用于使用类的同一个实例(对象)作为锁对象。下面情况不适用:
    若上述代码中第26行和第39行,改为
    new Printer().output(“<相应字符串>”);
    则无法使用本方法实现线程互斥,而须采用第2种方法。

  2. 以Outputer.class作为锁对象
    Outputer类的字节码对象由jvm自动加载。
    实现方式
    synchronized(Outputer.class){…}
    static synchronized方法
    适用情况
    适用于整个类的所有对象都需要互斥访问的情况。

  3. 以自定义的对象作为所对象
    实现方式
    synchronized(<自定义锁对象>){…}
    适用情况
    同一个类中代码块或者方法的互斥,一般可以用第1种和第2种方法替换。当出现需要在多个类(或者多个类的实例)之间进行互斥控制时,可能需要采用本方法。

来自: http://blog.csdn.net//kingzone_2008/article/details/48166731