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

    xiaoxiao2021-12-14  20

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

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

    [java]  view plain  copy  print ? static class Outputer {              public void output(String name) {               int len = name.length();                  for (int i = 0; i < len; i++) {                   System.out.print(name.charAt(i));               }               System.out.println();              }   }  

         1.2  测试代码

    [java]  view plain  copy  print ? public class TestThread {       static final Outputer outputer = new Outputer();          public static void main(String[] args) {           new Thread(new Runnable() {               @Override               public void run() {                   while (true) {                       try {                           Thread.sleep(10);                       } catch (InterruptedException e) {                           e.printStackTrace();                       }                       outputer.output("zhangzhang");                   }                  }           }).start();              new Thread(new Runnable() {               @Override               public void run() {                   while (true) {                       try {                           Thread.sleep(10);                       } catch (InterruptedException e) {                           e.printStackTrace();                       }                       outputer.output("lilili");                   }                  }           }).start();          }   }  

          1.3 测试结果

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

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

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

    [java]  view plain  copy  print ? static class Outputer {           private String xx = "";              public void output(String name) {               int len = name.length();               synchronized (xx) {                   for (int i = 0; i < len; i++) {                       System.out.print(name.charAt(i));                   }                   System.out.println();               }           }   }          在测试代码里面,只new了一个Outputer对象,在两个Runable接口里面调用。这样才能保证用的字符串Sting的xxx是唯一的.

         2.2 测试结果

                正常

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

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

    [java]  view plain  copy  print ? static class Outputer {              public void output(String name) {               int len = name.length();               synchronized (this) {                   for (int i = 0; i < len; i++) {                       System.out.print(name.charAt(i));                   }                   System.out.println();               }           }   }             因为每个类的方法里面,本身就有一个this对象,所以不需要再使用一个新的字符串.直接使用this对象就好了。

         3.2 测试结果

                正常

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

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

    [java]  view plain  copy  print ? static class Outputer {              public void output(String name) {               int len = name.length();               synchronized (Outputer.class) {                   for (int i = 0; i < len; i++) {                       System.out.print(name.charAt(i));                   }                   System.out.println();               }           }   }  

         4.2 测试结果

                正常

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

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

    [java]  view plain  copy  print ? static class Outputer {              public synchronized void output(String name) {               int len = name.length();               for (int i = 0; i < len; i++) {                   System.out.print(name.charAt(i));               }               System.out.println();           }   }  

          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 测试结果

               正常

    转载请注明原文地址: https://ju.6miu.com/read-964604.html

    最新回复(0)