传统线程互斥技术

  1 public class TraditionalThreadSynchronized {
  2     
  3     /**
  4      * @param args
  5      */
  6     public static void main(String[] args) {
  7         new TraditionalThreadSynchronized().init();
  8     }
  9     
 10     private void init() {
 11         /**
 12          * 静态方法中不能NEW内部类的实例对象
 13          * 内部类可以访问外部类的成员变量,对象创建完了,成员变量才分配空间
 14          * Outputer在静态方法中创建的时候还没有TraditionalThreadSynchronized对象
 15          */
 16         final Outputer outputer = new Outputer();
 17         new Thread(new Runnable() {
 18             @Override
 19             public void run() {
 20                 while (true) {
 21                     try {
 22                         Thread.sleep(10);
 23                     }
 24                     catch (InterruptedException e) {
 25                         e.printStackTrace();
 26                     }
 27                     outputer.output("wunanjie1234");
 28                 }
 29                 
 30             }
 31         }).start();
 32         
 33         new Thread(new Runnable() {
 34             @Override
 35             public void run() {
 36                 while (true) {
 37                     try {
 38                         Thread.sleep(10);
 39                     }
 40                     catch (InterruptedException e) {
 41                         e.printStackTrace();
 42                     }
 43                     outputer.output2("lihuoming1234");
 44                 }
 45                 
 46             }
 47         }).start();
 48         
 49     }
 50     
 51     /**
 52      * 
 53      * @描述: 将名字打印在控制台上 .
 54      * 加static相当于外部类
 55      * @作者: Wnj .
 56      * @创建时间: 2017年5月15日 .
 57      * @版本: 1.0 .
 58      */
 59     static class Outputer {
 60         
 61         /**
 62          *打印错乱
 63          * wunanlihuoming1234
 64          *   jie1234
 65             
 66             
 67          * 要实现原子性
 68          * 当有一个线程在执行该方法的时候,其它线程不能执行
 69          * <功能详细描述>
 70          * @param name
 71          */
 72         public void output(String name) {
 73             int len = name.length();
 74             //把你要保护起来的代码用synchronized关键字保护起来,进行排它性,只有当这个线程出来后,其它线程才能进去
 75             //锁一定要是同一个对象
 76             synchronized (Outputer.class) {
 77                 for (int i = 0; i < len; i++) {
 78                     System.out.print(name.charAt(i));
 79                 }
 80                 System.out.println();
 81             }
 82         }
 83         
 84         public synchronized void output2(String name) {
 85             int len = name.length();
 86             for (int i = 0; i < len; i++) {
 87                 System.out.print(name.charAt(i));
 88             }
 89             System.out.println();
 90         }
 91         /**
 92          * 类的字节码在内存中也算是一份对象,静态方法运行的时候不需要创建类的实例对象,使用Outputer.class字节码就可以进行互斥
 93          * <功能详细描述>
 94          * @param name
 95          */
 96         public static synchronized void output3(String name) {
 97             int len = name.length();
 98             for (int i = 0; i < len; i++) {
 99                 System.out.print(name.charAt(i));
100             }
101             System.out.println();
102         }
103     }
104 }

 

posted @ 2017-05-25 09:57  superGG  阅读(195)  评论(0编辑  收藏  举报