这个就相当于Qt5里面的 QMutex互斥量,在一个线程 使用一个公共资源之前 调用 mutex.lock(),锁住后只允许当前线程调用此资源 其余线程等待,然后 调用 mutex.unlock()解锁,解锁后大家都可以抢,然后又有一个锁定。。。
HanThread.java
package threadsafe; public class HanThread extends Thread{ private static int i; static{ i=0; } public static int getI(){ return HanThread.i; } public HanThread() { // TODO Auto-generated constructor stub } @Override public void run() { // TODO Auto-generated method stub int i=0; while(i<10) { HanThread.i=HanThread.i+1; Singleton.Printf(); i++; } } }Singleton.java package threadsafe; public class Singleton { public static void Printf(){ System.out.println("A"); System.out.println("b"); System.out.println("C"); System.out.println("D"); System.out.println("E"); System.out.println("F"); System.out.println("G"); System.out.println("H"); System.out.println("I"); System.out.println("J"); System.out.println("K"); System.out.println("L"); System.out.println(HanThread.getI()); } } HanEntity.java package threadsafe; public class HanEntity { public static void main(String[] args) { HanThread thread1=new HanThread(); HanThread thread2=new HanThread(); thread1.start(); thread2.start(); } } 运行结果: A b C D E A b C D E F G H I J K L 2 A b C D E F G F G H I J K L 3 A b C D E F G H I J K L 4 A b C D E F G H I J K L 5 A b C D E F G H I J K L 6 A b C D E F G H H I J K L 7 A b C D E F G H I J K L 8 A b C D E F G H I J K L 9 A b C D E F G H I J K L 10 A b C D E F G H I J K L 11 A b C D E F G H I J K L 12 A b C D E F G H I J K L 13 A b C D E F G H I J K L 14 A b C D E F G H I J K L 15 A b C D E F G I J K L 16 A b C D E F G H I J K L H I J K L 17 17 A b C D E F G H I J K L 18 A b C D E F G H I J K L 19 A b C D E F G H I J K L 20 使用了 synchronized的版本HanThread.java
package threadsafe; public class HanThread extends Thread{ private static int i; static{ i=0; } public static int getI(){ return HanThread.i; } public HanThread() { // TODO Auto-generated constructor stub } @Override public void run() { // TODO Auto-generated method stub int i=0; while(i<10) { HanThread.i=HanThread.i+1; Singleton.Printf(); i++; } } }Singleton.javapackage threadsafe; public class Singleton { public static synchronized void Printf(){ System.out.println("A"); System.out.println("b"); System.out.println("C"); System.out.println("D"); System.out.println("E"); System.out.println("F"); System.out.println("G"); System.out.println("H"); System.out.println("I"); System.out.println("J"); System.out.println("K"); System.out.println("L"); System.out.println(HanThread.getI()); } } HanEntity.java package threadsafe; public class HanEntity { public static void main(String[] args) { HanThread thread1=new HanThread(); HanThread thread2=new HanThread(); thread1.start(); thread2.start(); } } 运行结果: A b C D E F G H I J K L 2 A b C D E F G H I J K L 3 A b C D E F G H I J K L 4 A b C D E F G H I J K L 4 A b C D E F G H I J K L 5 A b C D E F G H I J K L 6 A b C D E F G H I J K L 8 A b C D E F G H I J K L 9 A b C D E F G H I J K L 10 A b C D E F G H I J K L 11 A b C D E F G H I J K L 12 A b C D E F G H I J K L 13 A b C D E F G H I J K L 14 A b C D E F G H I J K L 14 A b C D E F G H I J K L 15 A b C D E F G H I J K L 16 A b C D E F G H I J K L 17 A b C D E F G H I J K L 18 A b C D E F G H I J K L 19 A b C D E F G H I J K L 20
