1. 程式人生 > >Java多執行緒學習筆記(四) 使用ReentrantLock實現同步

Java多執行緒學習筆記(四) 使用ReentrantLock實現同步

1. 測試1

1.1 MyService

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
    private Lock lock = new ReentrantLock();

    public void testMethod(){
        lock.lock();

        for (int i = 0; i < 3; i++){
            System.
out.println("ThreatName = " + Thread.currentThread().getName() + " " + i); } lock.unlock(); } }

1.2 MyThread

public class MyThread extends Thread {

    private MyService service;

    public MyThread(MyService service){
        super();
        this.service =
service; } @Override public void run(){ service.testMethod(); } }

1.3 Test

public class Test {
    public static void main(String[] args) {

        MyService service = new MyService();

        MyThread t1 = new MyThread(service);
        MyThread t2 = new MyThread(service)
; MyThread t3 = new MyThread(service); t1.start(); t2.start(); t3.start(); } }

1.4 執行結果

ThreatName = Thread-0 0
ThreatName = Thread-0 1
ThreatName = Thread-0 2
ThreatName = Thread-2 0
ThreatName = Thread-2 1
ThreatName = Thread-2 2
ThreatName = Thread-1 0
ThreatName = Thread-1 1
ThreatName = Thread-1 2

2. 測試2

2.1 MyService

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {

    private Lock lock = new ReentrantLock();

    public void methodA(){
        try {
            lock.lock();
            System.out.println("methodA begin, ThreadName = " + Thread.currentThread().getName());

            Thread.sleep(3000);

            System.out.println("methodA end  , ThreadName = " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void methodB(){
        try {
            lock.lock();
            System.out.println("methodB begin, ThreadName = " + Thread.currentThread().getName());

            Thread.sleep(3000);

            System.out.println("methodB end  , ThreadName = " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}

2.2 ThreadA

public class ThreadA extends Thread{
    private MyService service;

    public ThreadA(MyService service){
        super();
        this.service = service;
    }

    @Override
    public void run(){
        service.methodA();
    }
}

2.3 ThreadAA

public class ThreadAA extends Thread{
    private MyService service;

    public ThreadAA(MyService service){
        super();
        this.service = service;
    }

    @Override
    public void run(){
        service.methodA();
    }
}

2.4 ThreadB

public class ThreadB extends Thread{
    private MyService service;

    public ThreadB(MyService service){
        super();
        this.service = service;
    }

    @Override
    public void run(){
        service.methodB();
    }
}

2.5 Test

public class Test {
    public static void main(String[] args) throws InterruptedException {
        MyService service = new MyService();

        //A thread
        ThreadA a = new ThreadA(service);
        a.setName("A");
        a.start();

        //AA thread
        ThreadAA aa = new ThreadAA(service);
        aa.setName("AA");
        aa.start();
        Thread.sleep(1000);

        //B thread
        ThreadB b = new ThreadB(service);
        b.setName("B");
        b.start();
    }
}

2.6 執行結果

methodA begin, ThreadName = A
methodA end  , ThreadName = A
methodA begin, ThreadName = AA
methodA end  , ThreadName = AA
methodB begin, ThreadName = B
methodB end  , ThreadName = B

呼叫lock(),執行緒就有了物件監視器,其他的執行緒只能等待鎖再次釋放,效果和synchronized一樣,執行緒還是順序執行的。