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

📄 bothinmethod.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
public class BothInMethod extends Object {
	private String objID;

	public BothInMethod(String objID) {
		this.objID = objID;
	}

	public void doStuff(int val) {
		print("entering doStuff()");
		int num = val * 2 + objID.length();
		print("in doStuff() - local variable num=" + num);

		// slow things down to make observations
		try { Thread.sleep(2000); } catch ( InterruptedException x ) { }

		print("leaving doStuff()");
	}

	public void print(String msg) {
		threadPrint("objID=" + objID + " - " + msg);
	}

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

	public static void main(String[] args) {
		final BothInMethod bim = new BothInMethod("obj1");

		Runnable runA = new Runnable() {
				public void run() {
					bim.doStuff(3);
				}
			};

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

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

		Runnable runB = new Runnable() {
				public void run() {
					bim.doStuff(7);
				}
			};

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

⌨️ 快捷键说明

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