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

📄 testthread3.java

📁 该文件在eclipse环境上运行
💻 JAVA
字号:
package test.thread;

public class TestThread3 {
	public static void main(String[] args) throws InterruptedException {
		TestSynchronized t = new TestSynchronized();
		t.add.start();
		t.sub.start();
		Thread.sleep(6000);
		System.out.println("i = " + t.i);
	}
}
class TestSynchronized{
	int i = 200;
	Thread add;
	Thread sub;
	public TestSynchronized(){
		add = new Thread(){
			public void run(){
				for(int j=0; j<100; j++){
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					add();
				}
			}
		};
		sub = new Thread(){
			public void run(){
				for(int j=0; j<100; j++){
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					sub();
				}
			}
		};
	}
	public synchronized void add(){
		i = i+1;
	}
	public synchronized void sub(){
		i = i-1;
	}
}

⌨️ 快捷键说明

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