All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.dm.LockObject Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.dm;

/**
 * ??????
 * @author WangXiaoJun
 *
 */
class LockObject implements IResource {
	private volatile Thread thread; // ??ǰ???????߳?
 
	public LockObject(Context ctx) {
	}
	
	public synchronized void close() {
		if (thread != null) { // Thread.currentThread()
			thread = null;
			notify();
		}
	}
	
	/**
	 * ?????˶???
	 * @param ms ?ȴ?????????С??0??ʾ??????ʱ
	 * @param ctx ??????????
	 * @return true???ɹ???false??ʧ??
	 */
	public synchronized boolean lock(long ms, Context ctx) {
		Thread cur = Thread.currentThread();
		if (thread == null) {
			thread = cur;
			ctx.addResource(this);
			return true;
		} else if (thread == cur) {
			return false;
		} else {
			try {
				if (ms >= 0) {
					wait(ms); 
					if (thread != null) {
						return false;
					}
				} else {
					// ?????????????̵߳???unlock??????ֽ?????lock????̵߳?wait?ű?????
					do {
						wait();
					} while (thread != null);
				}
				
				thread = cur;
				ctx.addResource(this);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
	}
	
	/**
	 * ????
	 * @param ctx ??????????
	 * @return true???ɹ???false??ʧ??
	 */
	public synchronized boolean unlock(Context ctx) {
		if (thread == null) {
			return true;
		} else if (thread != Thread.currentThread()) {
			return false;
		} else {
			thread = null;
			ctx.removeResource(this);
			notify();
			return true;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy