⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistentresource.java

📁 这个是perst-269.zip下面的SOURCECODE,和大家分享了。
💻 JAVA
字号:
package org.garret.perst;

/**
 * Base class for persistent capable objects supporting locking
 */

public class PersistentResource extends Persistent implements IResource {
    public synchronized void sharedLock() {
        Thread currThread = Thread.currentThread();
        try { 
            while (true) { 
                if (owner == currThread) { 
                    nWriters += 1;
                    break;
                } else if (nWriters == 0) { 
                    if (storage == null || storage.lockObject(this)) { 
                        nReaders += 1;
                    }
                    break;
                } else { 
                    wait();
                }
            }
        } catch (InterruptedException x) { 
            throw new StorageError(StorageError.LOCK_FAILED);
        }
    }
                    
    public boolean sharedLock(long timeout) {
        Thread currThread = Thread.currentThread();
        long startTime = System.currentTimeMillis();
        synchronized (this) { 
            try { 
                while (true) { 
                    if (owner == currThread) { 
                        nWriters += 1;
                        return true;
                    } else if (nWriters == 0) { 
                        if (storage == null || storage.lockObject(this)) { 
                            nReaders += 1;
                        }
                        return true;
                    } else { 
                        long currTime = System.currentTimeMillis();
                        if (currTime < startTime) { 
                            currTime = startTime;
                        }
                        if (startTime + timeout <= currTime) { 
                            return false;
                        }
                        wait(startTime + timeout - currTime);
                    }
                }
            } catch (InterruptedException x) { 
                return false;
            }
        }
    }
                    
    public synchronized void exclusiveLock() {
        Thread currThread = Thread.currentThread();
        try { 
            while (true) { 
                if (owner == currThread) { 
                    nWriters += 1;
                    break;
                } else if (nReaders == 0 && nWriters == 0) { 
                    nWriters = 1;
                    owner = currThread;
                    if (storage != null) { 
                        storage.lockObject(this);
                    }
                    break;
                } else { 
                    wait();
                }
            }
        } catch (InterruptedException x) { 
            throw new StorageError(StorageError.LOCK_FAILED);
        }
    }
                    
    public boolean exclusiveLock(long timeout) {
        Thread currThread = Thread.currentThread();
        long startTime = System.currentTimeMillis();
        synchronized (this) { 
            try { 
                while (true) { 
                    if (owner == currThread) { 
                        nWriters += 1;
                        return true;
                    } else if (nReaders == 0 && nWriters == 0) { 
                        nWriters = 1;
                        owner = currThread;
                        if (storage != null) { 
                            storage.lockObject(this);
                        }
                        return true;
                    } else { 
                        long currTime = System.currentTimeMillis();
                        if (currTime < startTime) { 
                            currTime = startTime;
                        }
                        if (startTime + timeout <= currTime) { 
                            return false;
                        }
                        wait(startTime + timeout - currTime);
                    }
                }
            } catch (InterruptedException x) { 
                return false;
            }
        }
    }
                   
    public synchronized void unlock() { 
        if (nWriters != 0) { 
            if (--nWriters == 0) { 
                owner = null;
                notifyAll();
            }
        } else if (nReaders != 0) { 
            if (--nReaders == 0) { 
                notifyAll();
            }
        }
    }

    public synchronized void reset() { 
        if (nWriters > 0) { 
            nWriters = 0;
            nReaders = 0;
            owner = null;
        } else if (nReaders > 0) { 
            nReaders -= 1;
        }
        notifyAll();
    }

    public PersistentResource() {}
    
    public PersistentResource(Storage storage) { 
        super(storage);
    }

    private transient Thread owner;
    private transient int    nReaders;
    private transient int    nWriters;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -