countingthread.java

来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 47 行

JAVA
47
字号
// Chapter 7, Listing 7
public class CountingThread implements Runnable
{
	Counter myCounter;
	int     countAmount;

	// Construct a counting thread to use the specified counter
	public CountingThread (Counter counter, int amount)
	{
		myCounter = counter;
		countAmount = amount;
	}

	public void run()
	{
		// Increase the counter the specified number of times
		for (int i = 1; i <= countAmount; i++)
		{
			// Increase the counter
			myCounter.increaseCount();
		}
	}

	public static void main(String args[]) throws Exception
	{
		// Create a new, thread-safe counter
		Counter c = new Counter();

		// Our runnable instance will increase the counter
		// ten times, for each thread that runs it
		Runnable runner = new CountingThread( c, 10 );

		System.out.println ("Starting counting threads");

		Thread t1 = new Thread(runner);
		Thread t2 = new Thread(runner);
		Thread t3 = new Thread(runner);
		t1.start(); t2.start(); t3.start();

		// Wait for all three threads to finish
		t1.join(); t2.join(); t3.join();

		System.out.println ("Counter value is " + c.getCount() );
	}
}

⌨️ 快捷键说明

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