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

📄 synchronizedboolean.java

📁 FMJ(freedom media for java)是java视频开发的新选择
💻 JAVA
字号:
package com.lti.utils.synchronization;/** * Useful class for setting a flag between threads.  For simple uses, this class is  * not that useful, since boolean access is guaranteed to be atomic. *  * @author Ken Larson */ public class SynchronizedBoolean{	private boolean b = false;		public SynchronizedBoolean()	{	super();	}	public SynchronizedBoolean(boolean initValue)	{		this.b = initValue;	}		public synchronized boolean getValue()	{		return b;	}	public synchronized void setValue(boolean newValue)	{		if (b != newValue)		{	b = newValue;			notifyAll();		}			}	public synchronized void waitUntil(boolean value) throws InterruptedException	{	while (b != value)		{			wait();		}				}	/**	 * @return true if value matches value, false if timeout occurred	 */	public synchronized boolean waitUntil(boolean value, int timeout) throws InterruptedException	{	long start = System.currentTimeMillis();		while (b != value)		{	long now = System.currentTimeMillis();			long diff = now - start;			long wait = timeout - diff;			if (wait <= 0)				return false;			wait(wait);		}		return true;		}			/**	 * 	 * If the value is oldValue, set to newValue and return true, otherwise return false.	 */	public synchronized boolean getAndSet(boolean oldValue, boolean newValue)	{		if (b != oldValue)			return false;		setValue(newValue);		return true;	}	}

⌨️ 快捷键说明

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