Java多线程之《自定义锁实现》

Mutex

Mutex为自定义独占锁。Sync为静态内部类,自定义同步器,继承AbstractQueuedSynchronizer。

package concurrent;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;

/**
 * Desc:自定义独占锁
 * Creator: pengweixiang
 * Date: 2019-04-07
 */
public class Mutex implements Lock
{
    /**
     * 静态内部类,自定义同步器
     */
    private static class Sync extends AbstractQueuedSynchronizer
    {
        /**
         * 是否为占用状态
         *
         * @return boolean
         */
        @Override
        protected boolean isHeldExclusively()
        {
            return getState() == 1;
        }

        /**
         * 获取锁
         *
         * @param acquires
         * @return
         */
        @Override
        protected boolean tryAcquire(int acquires)
        {
            if (compareAndSetState(0, 1))
            {
                setExclusiveOwnerThread(Thread.currentThread());
                return true;
            }
            return false;
        }

        /**
         * 释放锁
         *
         * @param release release
         * @return
         */
        @Override
        protected boolean tryRelease(int release)
        {
            if (getState() == 0)
            {
                throw new IllegalMonitorStateException();
            }
            setExclusiveOwnerThread(null);
            setState(0);
            return true;
        }

        /**
         * 返回一个Condition,每个condition都包含一个condition队列
         *
         * @return condition
         */
        Condition newCondition()
        {
            return new ConditionObject();
        }
    }

    private final Sync sync = new Sync();

    public void lock()
    {
        sync.acquire(1);
    }

    public boolean tryLock()
    {
        return sync.tryAcquire(1);
    }

    public void unlock()
    {
        sync.release(1);
    }

    public Condition newCondition()
    {
        return sync.newCondition();
    }

    public boolean isLock()
    {
        return sync.isHeldExclusively();
    }

    public boolean hasQueuedThread()
    {
        return sync.hasQueuedThreads();
    }

    public void lockInterruptibly() throws InterruptedException
    {
        sync.acquireInterruptibly(1);
    }

    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException
    {
        return sync.tryAcquireNanos(1, unit.toNanos(timeout));
    }
}

MutexTest

package concurrent;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Desc:
 * Creator: pengweixiang
 * Date: 2019-04-14
 */
public class MutexTest
{
    private static Lock lock = new Mutex();

    public static void main(String[] args)
    {
        MutexTest mutexTest = new MutexTest();
        ThreadTask threadTask1 = mutexTest.new ThreadTask("Task 1");
        ThreadTask threadTask2 = mutexTest.new ThreadTask("Task 2");
        ThreadTask threadTask3 = mutexTest.new ThreadTask("Task 3");
        threadTask1.start();
        threadTask2.start();
        threadTask3.start();
    }

    class ThreadTask extends Thread
    {
        private SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

        public ThreadTask(String name)
        {
            super(name);
        }

        @Override
        public void run()
        {
            lock.lock();
            try
            {
                System.out.println(Thread.currentThread().getName() + ", Time: " + sdf.format(new Date()));
                TimeUtils.sleep(5);
            }
            finally
            {
                lock.unlock();
            }
        }
    }
}

运行结果

每个线程运行5秒钟,才执行下一个线程。
Java多线程之《自定义锁实现》_第1张图片

你可能感兴趣的:(Java,并发编程,Java,锁机制)