自定义一把锁,实现四元共享

2020-01-17  本文已影响0人  全麦土司
//MyLock.java
package com.cym.mylock;


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

/*
自定义的锁
 */
public class MyLock implements Lock {


    public class TwinsLock extends AbstractQueuedSynchronizer {

        //这是一个共享的锁
        @Override
        protected int tryAcquireShared(int arg) {
            for(;;){
                if (getState() >= arg)
                    return -1;
                int tmp = getState();
                if (compareAndSetState(tmp,tmp+1))
                    return 1;
            }

        }

        @Override
        protected boolean tryReleaseShared(int arg) {
            for (;;) {

                int tmp = getState();
                if (compareAndSetState(tmp,tmp-1))
                    return true;
            }

        }

        @Override
        public String toString() {
            return super.toString();
        }
    }


    public TwinsLock lock = new TwinsLock();//声明自定义的锁



    @Override
    public void lock() {
        lock.acquireShared(4);//表示支持两个线程
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        lock.acquireSharedInterruptibly(4);
    }

    @Override
    public boolean tryLock() {
        return lock.tryAcquireShared(4)>0;//返回的结果是不是大于0
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return lock.tryAcquireSharedNanos(4,time);
    }

    @Override
    public void unlock() {
        lock.releaseShared(4);
    }

    @Override
    public Condition newCondition() {
        return newCondition();
    }




}


测试代码:

package com.cym.mylock;



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

/*
对锁进行测试
 */
public class MyLockTest {
    public static Lock lock = new MyLock();
    static class Worker extends Thread {
        @Override
        public void run() {
            while(true) {
                lock.lock();

                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    lock.unlock();
                }

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //启动十个线程
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Worker worker = new Worker();
            worker.setDaemon(true);
            worker.start();
        }


        for (int i = 0; i < 10; i++) {
            Thread.sleep(7000);
//            System.out.println();
        }

    }


}



结果是按照每四个进行一次输出。

上一篇下一篇

猜你喜欢

热点阅读