一、采用synchronized关键字。
实现用时就是放该关键字到方法前面,表示该方法是同步的,同一时间只能有一个线程使用它。但是有区别:
1、静态方法前加synchronized关键字表示所有此类的不同实例调用这个方法的时候都是串行的。
2、非静态方法前标识某一个实例被不同的线程调用时是串行的,但是不同实例之间是并行的。
例如:
- /**
- * 测试synchronized的区别
- *
- * @author <a href="mailto:HL_Qu@hotmail.com">Along</a>
- *
- * @version $Revision$
- *
- * @since 2009-3-24
- */
- public class TestSynchronized implements Runnable {
- /**
- * 所有类实例等待一个锁
- */
- private synchronized static void test() throws InterruptedException {
- System.out.println("Obj= in.");
- Thread.sleep(4000);
- System.out.println("Obj= out.");
- }
-
- /**
- * 同一个实例的不同线程等待一个锁
- */
- private synchronized void test2() throws InterruptedException {
- System.out.println("Obj=" + this + "in.");
- Thread.sleep(4000);
- System.out.println("Obj=" + this + "out.");
- }
- @Override
- public void run() {
- try {
- test();
- this.test2();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- TestSynchronized a = new TestSynchronized();
- TestSynchronized b = new TestSynchronized();
- new Thread(a).start();
- new Thread(b).start();
- }
- }
输出:
- Obj= in.
- Obj= out.
- Obj= in.
- Obj=TestSynchronized@61de33in.
- Obj= out.
- Obj=TestSynchronized@ca0b6in.
- Obj=TestSynchronized@61de33out.
- Obj=TestSynchronized@ca0b6out.
synchronized还有一种使用方式就是同步代码块。例如:
- /**
- * 模拟静态方法锁机制
- *
- * @author <a href="mailto:HL_Qu@hotmail.com">Along</a>
- *
- * @version $Revision$
- *
- * @since 2009-3-24
- */
- public class TestLock2 implements Runnable {
- private static String lock = new String("Locker");
- /**
- * 所有类实例等待一个锁
- */
- private void test() {
- System.out.println("Obj=" + this + "in.");
- synchronized (lock) {
- System.out.println("Obj=" + this + "Locked.");
- try {
- Thread.sleep(4000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Obj=" + this + "Unlocked.");
- }
- System.out.println("Obj=" + this + "out.");
- }
-
- /**
- * 同一个实例的不同线程等待一个锁
- */
- private void test2() {
- System.out.println("Obj=" + this + "in.");
- synchronized (this) {
- System.out.println("Obj=" + this + "Locked.");
- try {
- Thread.sleep(4000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Obj=" + this + "Unlocked.");
- }
- System.out.println("Obj=" + this + "out.");
- }
-
- @Override
- public void run() {
- this.test2();
- this.test();
- }
-
- public static void main(String[] args) {
- TestLock2 a = new TestLock2();
- TestLock2 b = new TestLock2();
- new Thread(a).start();
- new Thread(b).start();
- }
- }
输出:
- Obj=TestLock2@61de33in.
- Obj=TestLock2@61de33Locked.
- Obj=TestLock2@14318bbin.
- Obj=TestLock2@14318bbLocked.
- Obj=TestLock2@14318bbUnlocked.
- Obj=TestLock2@14318bbout.
- Obj=TestLock2@14318bbin.
- Obj=TestLock2@14318bbLocked.
- Obj=TestLock2@61de33Unlocked.
- Obj=TestLock2@61de33out.
- Obj=TestLock2@61de33in.
- Obj=TestLock2@14318bbUnlocked.
- Obj=TestLock2@14318bbout.
- Obj=TestLock2@61de33Locked.
- Obj=TestLock2@61de33Unlocked.
- Obj=TestLock2@61de33out.
二、采用Java提供的并发类实现。
例如使用ReentrantLock(一个可重入的互斥锁 Lock,它具有与使用 synchronized 方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大。)
代码:
- import java.util.concurrent.locks.ReentrantLock;
- /**
- * 用Java提供的工具类模拟静态方法锁机制
- *
- *
- * @author <a href="mailto:HL_Qu@hotmail.com">Along</a>
- *
- * @version $Revision$
- *
- * @since 2009-3-24
- */
- public class TestLock implements Runnable {
- private static ReentrantLock lock = new ReentrantLock();
- @Override
- public void run() {
- System.out.println("Obj=" + this + "in.");
- if (lock.isLocked()) {
- System.out.println("Method has been Locked.");
- }
-
- lock.lock();
- System.out.println("Obj=" + this + "Locked.");
- try {
- Thread.sleep(4000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Obj=" + this + "Unlocked.");
- lock.unlock();
-
- System.out.println("Obj=" + this + "out.");
- }
-
- public static void main(String[] args) {
- TestLock a = new TestLock();
- TestLock b = new TestLock();
- new Thread(a).start();
- new Thread(b).start();
- }
- }
输出:
- Obj=TestLock@61de33in.
- Obj=TestLock@61de33Locked.
- Obj=TestLock@14318bbin.
- Method has been Locked.
- Obj=TestLock@61de33Unlocked.
- Obj=TestLock@14318bbLocked.
- Obj=TestLock@61de33out.
- Obj=TestLock@14318bbUnlocked.
- Obj=TestLock@14318bbout.