synchronized使用之基本原则:
synchronized可以锁方法,也可以锁代码片段,但要实现互斥的基本就是在想要互斥的代码上加”同一把锁“,也就是同一个对象,也就是用==判断等于true的对象
下面看一个例子:
Work.java 真正做事情的类
1 package com.yzl; 2 3 public class Work { 4 /** 5 * 未同步的 6 * @param name 7 */ 8 public void noSynwrite(String name){ 9 for(int i=0; i
测试类ThreadPart_2.java
1 package com.yzl; 2 3 /** 4 * 传统的线程互斥 5 * 原理:使用同一个对象(锁)才能实现, 6 * 静态方法的同步则只能使用类的字节码也就是ClassName.class来做锁 7 * @author yzl 8 * 9 */10 public class ThreadPart_2 {11 public static void main(String[] args) {12 noSyn();13 synWithOneObj();14 synWithOneStaticObj();15 }16 17 /**18 * 未线程同步的19 */20 private static void noSyn(){21 final Work work = new Work();22 23 Thread thread = new Thread(new Runnable() {24 @Override25 public void run() {26 while(true){27 work.noSynwrite("wangwu");28 }29 }30 });31 thread.start();32 33 Thread thread1 = new Thread(new Runnable() {34 @Override35 public void run() {36 while(true){37 work.noSynwrite("zhangsan");38 }39 }40 });41 thread1.start();42 }43 44 /**45 * 使用同一个实例对象做为锁的46 */47 private static void synWithOneObj(){48 final Work work = new Work();49 50 Thread thread = new Thread(new Runnable() {51 @Override52 public void run() {53 while(true){54 work.write1("wangwu");55 }56 }57 });58 thread.start();59 60 Thread thread1 = new Thread(new Runnable() {61 @Override62 public void run() {63 while(true){64 work.write2("zhangsan");65 }66 }67 });68 thread1.start();69 }70 71 /**72 * 使用同一个类的字节码做为锁的73 */74 private static void synWithOneStaticObj(){75 Thread thread = new Thread(new Runnable() {76 @Override77 public void run() {78 while(true){79 new Work().noSynwrite("wangwu");80 }81 }82 });83 thread.start();84 85 Thread thread1 = new Thread(new Runnable() {86 @Override87 public void run() {88 while(true){89 Work.write3("zhangsan");90 }91 }92 });93 thread1.start();94 }95 }
将上面ThreadPart_2的第12、13、14行代码分别依次执行,
第12行代码将出现如下类似结果:
//运行结果部分示例zhangsanzhwuwangwuwangangsanzhangsawangwu
第13和14行将是正常的打印各自的名字