Java同步:无阻塞锁定

Mar*_*sen 2 java concurrency locking

我有一个同步练习,我需要同步一个read方法,这样只要没有执行写方法,任何数量的线程都可以执行它.这必须从头开始,所以我不能使用java.util.concurrent.locks等.

为此,我需要某种机制来保护,但不阻止读取方法,因此读取线程被写入阻止,但不被其他读取阻塞.我无法使用普通锁定,因为在read方法中调用lock方法会导致其他读取线程等待.

规则应该是这样的:当一个线程在write()内部时,没有其他线程必须输入read()或write()当一个线程在read()内部时,没有其他线程必须输入write(),但它们可能会进入读()

我尝试过构建一些自制的锁来解决这个问题.WriteLock是一个相当标准的reenterant锁,除非它正在执行读取时阻塞(使用readcounter)ReadLock应该只导致线程等待,如果遍历write().否则它应该只允许线程进行其业务并增加WriteLocks计数器.

码:

package sync;

public class SyncTest {
    Long testlong = new Long(0L);
    int reads = 0;
    int writes = 0;
    WriteLock w = new WriteLock();
    ReadLock r = new ReadLock(w);

    public SyncTest() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String args[]){

        final SyncTest s = new SyncTest();

        for(int i = 0 ; i<3 ; i++){ //Start a number of threads to attack SyncTest
            final int ifinal = i;
            new Thread(){
                int inc = ifinal;
                @Override
                public void run() {
                    System.out.println("Starting "+inc);
                    long starttime = System.currentTimeMillis();
                    try {
                    while(System.currentTimeMillis()-starttime < 10){

                        if (inc < 2){

                            s.readLong();

                        }else{
                            s.writeLong(inc+1);
                        }
                    }
                    System.out.println(inc + " done");
                    if(inc == 0){
                        Thread.sleep(1000);
                        System.out.println(s.reads+" "+s.writes);
                    }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // TODO Auto-generated method stub

                }
                @Override
                public String toString() {
                    // TODO Auto-generated method stub
                    return "Thread "+inc+" "+super.toString();
                }



            }.start();
        }
    }

    public Long readLong() throws InterruptedException{

        Long l;
        r.lock(); //Lock for reading
        //System.out.println("Read "+reads);
        l =  testlong;
        reads++;
        r.unlock(); //Unlock after reading
        return l;   
        }

    public void writeLong(int i) throws InterruptedException{

        w.lock(); //Lock for writing
        //System.out.println("Write "+writes);
        int curreads = reads;
        int curwrites = writes;
        testlong = testlong + i;
        writes++;

        Thread.sleep(100); //Simulate long write
        if(curreads != reads){
            System.out.println("Reads did not lock");
        }

        if(curwrites+1 != writes){
            System.out.println("Writes did not lock");
        }
        w.unlock(); //Unlock writing
    }

    protected class WriteLock{
        boolean isLocked = false;
        Thread lockedBy = null;
        int lockedCount = 0;
        int readers = 0; //The number of readers currently through the reading lock.

        public synchronized void lock() throws InterruptedException {
            System.out.println("Locking: "+Thread.currentThread());
            Thread callingThread = Thread.currentThread();
            while ((isLocked && lockedBy != callingThread) || readers > 0) { //Wait if locked or readers are in read()
                wait();
            }
            isLocked = true;
            lockedCount++;
            lockedBy = callingThread;
            System.out.println("Is locked: "+Thread.currentThread());
        }

        public synchronized void unlock() {
            //System.out.println("Unlocking: "+Thread.currentThread());
            if (Thread.currentThread() == this.lockedBy) {
                lockedCount--;

                if (lockedCount == 0) {
                    System.out.println("Is unlocked: "+Thread.currentThread());
                    isLocked = false;
                    notify();
                }
            }
        }

    }

    protected class ReadLock{
        WriteLock lock;

        public ReadLock(WriteLock lock) {
            super();
            this.lock = lock;
        }

        public synchronized void lock() throws InterruptedException { //If write() then wait
            System.out.println("Waiting to read: "+Thread.currentThread());
            Thread callingThread = Thread.currentThread();
            while (lock.isLocked && lock.lockedBy != callingThread) {
                wait();
            }
            lock.readers++; //Increment writelocks readers
            System.out.println("Reading: "+Thread.currentThread());

        }

        public synchronized void unlock() {
            lock.readers--; //Subtract from writelocks readers
            notify();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,读取锁到目前为止工作时它会在线程写入时锁定读取器,但在WriteLock解锁时它不会释放它们,据我所知.

这只是概念上没有声音,还是我不了解显示器?或者是其他东西?

Jon*_*eet 15

(在问题被编辑之前回答了它是一个练习.)

听起来你想要一个ReadWriteLock实现.

ReadWriteLock维护一对关联的锁,一个用于只读操作,另一个用于写入.只要没有写入器,读锁定可以由多个读取器线程同时保持.写锁是独占的.

一个实现是ReentrantReadWriteLock.

特别是对于并发性,在java.util.concurrent尝试实现自己的代码之前,总是值得查看现有的库(等).如果你和我一样,即使你可以在并发性方面做到正确,它也不会像专家写的代码那样高效......当然,这一切都是开始的;)

  • 不能同意这一点.我知道很多人都患有Not Invented Here综合症,但并发性很难; 我们不需要通过使用非战斗硬化的库来使其变得更难.(是的,这家伙碰巧必须从头开始写,但很多人在这里搜索也许不会.) (2认同)