Java中多线程互斥访问的实现

来源:互联网 发布:php apc缓存 编辑:程序博客网 时间:2024/05/17 23:56

1、没有实现线程互斥的函数

     1.1  没有实现多线程互斥访问的函数

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. static class Outputer {  
  2.   
  3.         public void output(String name) {  
  4.             int len = name.length();  
  5.   
  6.             for (int i = 0; i < len; i++) {  
  7.                 System.out.print(name.charAt(i));  
  8.             }  
  9.             System.out.println();  
  10.   
  11.         }  
  12. }  

     1.2  测试代码

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class TestThread {  
  2.     static final Outputer outputer = new Outputer();  
  3.   
  4.     public static void main(String[] args) {  
  5.         new Thread(new Runnable() {  
  6.             @Override  
  7.             public void run() {  
  8.                 while (true) {  
  9.                     try {  
  10.                         Thread.sleep(10);  
  11.                     } catch (InterruptedException e) {  
  12.                         e.printStackTrace();  
  13.                     }  
  14.                     outputer.output("zhangzhang");  
  15.                 }  
  16.   
  17.             }  
  18.         }).start();  
  19.   
  20.         new Thread(new Runnable() {  
  21.             @Override  
  22.             public void run() {  
  23.                 while (true) {  
  24.                     try {  
  25.                         Thread.sleep(10);  
  26.                     } catch (InterruptedException e) {  
  27.                         e.printStackTrace();  
  28.                     }  
  29.                     outputer.output("lilili");  
  30.                 }  
  31.   
  32.             }  
  33.         }).start();  
  34.   
  35.     }  
  36. }  

      1.3 测试结果

            出现不正常的输出情况,如下:



2、多线程互斥访问函数的实现一

     2.1 实现多线程互斥访问的函数 (在需要加上互斥访问的代码快上,加上synchronized关键字)

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. static class Outputer {  
  2.         private String xx = "";  
  3.   
  4.         public void output(String name) {  
  5.             int len = name.length();  
  6.             synchronized (xx) {  
  7.                 for (int i = 0; i < len; i++) {  
  8.                     System.out.print(name.charAt(i));  
  9.                 }  
  10.                 System.out.println();  
  11.             }  
  12.         }  
  13. }  
      在测试代码里面,只new了一个Outputer对象,在两个Runable接口里面调用。这样才能保证用的字符串Sting的xxx是唯一的.

     2.2 测试结果

            正常

3、多线程互斥访问函数的实现

      3.1 实现多线程互斥访问的函数(在需要加上互斥访问的代码快上,加上synchronized关键字,并使用this关键字)

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. static class Outputer {  
  2.   
  3.         public void output(String name) {  
  4.             int len = name.length();  
  5.             synchronized (this) {  
  6.                 for (int i = 0; i < len; i++) {  
  7.                     System.out.print(name.charAt(i));  
  8.                 }  
  9.                 System.out.println();  
  10.             }  
  11.         }  
  12. }  
         因为每个类的方法里面,本身就有一个this对象,所以不需要再使用一个新的字符串.直接使用this对象就好了。

     3.2 测试结果

            正常

4、多线程互斥访问函数的实现

     4.1 实现多线程互斥访问的函数(在需要加上互斥访问的代码快上,加上synchronized关键字,并使用类的字节码类)

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. static class Outputer {  
  2.   
  3.         public void output(String name) {  
  4.             int len = name.length();  
  5.             synchronized (Outputer.class) {  
  6.                 for (int i = 0; i < len; i++) {  
  7.                     System.out.print(name.charAt(i));  
  8.                 }  
  9.                 System.out.println();  
  10.             }  
  11.         }  
  12. }  

     4.2 测试结果

            正常

5、多线程互斥访问函数的实现

      5.1 实现多线程互斥访问的函数(在需要加上互斥访问的代码快上,在对应的函数上加上synchronized关键字)

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. static class Outputer {  
  2.   
  3.         public synchronized void output(String name) {  
  4.             int len = name.length();  
  5.             for (int i = 0; i < len; i++) {  
  6.                 System.out.print(name.charAt(i));  
  7.             }  
  8.             System.out.println();  
  9.         }  
  10. }  

      5.2 测试结果

            正常

6、多线程互斥访问函数的实现五

     6.1 实现多线程互斥访问的函数(使用Lock)

static class Outputer {ReentrantLock lock = new ReentrantLock();public void output(String name) {int len = name.length();try {// 上锁lock.lock();for (int i = 0; i < len; i++) {System.out.print(name.charAt(i));}System.out.println();} catch (Exception e) {} finally {// 释放锁lock.unlock();}}}

     6.2 测试结果

           正常


1 0