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

📄 staticblock.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
public class StaticBlock extends Object {
	public static synchronized void staticA() {
		System.out.println("entering staticA()");

		try { Thread.sleep(5000); } 
		catch ( InterruptedException x ) { }

		System.out.println("leaving staticA()");
	}

	public static void staticB() {
		System.out.println("entering staticB()");

		synchronized ( StaticBlock.class ) {
			System.out.println(
					"in staticB() - inside sync block");

			try { Thread.sleep(2000); } 
			catch ( InterruptedException x ) { }
		}

		System.out.println("leaving staticB()");
	}

	public static void main(String[] args) {
		Runnable runA = new Runnable() {
				public void run() {
					StaticBlock.staticA();
				}
			};

		Thread threadA = new Thread(runA, "threadA");
		threadA.start();

		try { Thread.sleep(200); } 
		catch ( InterruptedException x ) { }

		Runnable runB = new Runnable() {
				public void run() {
					StaticBlock.staticB();
				}
			};

		Thread threadB = new Thread(runB, "threadB");
		threadB.start();
	}
}

⌨️ 快捷键说明

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