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

📄 counter.java

📁 Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用安全
💻 JAVA
字号:
// Chapter 7, Listing 6
public class Counter 
{
	private int countValue;

	public Counter()
	{
		countValue = 0;
	}

	public Counter(int start)
	{
		countValue = start;
	}

	// Synchronized method to increase counter
	public synchronized void increaseCount()
	{
		int count = countValue;

		// Simulate slow data processing and modification
		// Removing the synchronized keyword will demonstrate
		// how inaccurate concurrent access can be. Adjusting
		// this value may be necessary on faster machines.
		try
		{
			Thread.sleep(5);
		}
		catch (InterruptedException ie) {}

		count = count + 1;

		countValue = count;
	}

	// Synchronized method to return counter value
	public synchronized int getCount()
	{
		return countValue;
	}
}

⌨️ 快捷键说明

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