testthread3.java
来自「该文件在eclipse环境上运行」· Java 代码 · 共 50 行
JAVA
50 行
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 + =
减小字号Ctrl + -
显示快捷键?