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

📄 staticneedsync.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
public class StaticNeedSync extends Object {
	private static int nextSerialNum = 10001;

	public static int getNextSerialNum() {
		int sn = nextSerialNum;

		// Simulate a delay that is possible if the thread 
		// scheduler chooses to swap this thread off the 
		// processor at this point. The delay is exaggerated 
		// for demonstration purposes.
		try { Thread.sleep(1000); } 
		catch ( InterruptedException x ) { }

		nextSerialNum++;
		return sn;
	}

	private static void print(String msg) {
		String threadName = Thread.currentThread().getName();
		System.out.println(threadName + ": " + msg);
	}

	public static void main(String[] args) {
		try {
			Runnable r = new Runnable() {
					public void run() {
						print("getNextSerialNum()=" + 
								getNextSerialNum());
					}
				};
			
			Thread threadA = new Thread(r, "threadA");
			threadA.start();
	
			Thread.sleep(1500); 
	
			Thread threadB = new Thread(r, "threadB");
			threadB.start();
	
			Thread.sleep(500); 
	
			Thread threadC = new Thread(r, "threadC");
			threadC.start();
	
			Thread.sleep(2500); 

			Thread threadD = new Thread(r, "threadD");
			threadD.start();
		} catch ( InterruptedException x ) {
			// ignore
		}
	}
}

⌨️ 快捷键说明

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