📄 threadexp3.java
字号:
/**
* @(#)ThreadExp3.java
*
*
* @author
* @version 1.00 2007/11/29
*/
public class ThreadExp3 {
/**
* Creates a new instance of <code>ThreadExp3</code>.
*/
public ThreadExp3() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Thread threadA,threadB,threadC,threadD;
TargetObject a1=new TargetObject(),a2=new TargetObject();
threadA=new Thread(a1);
threadB=new Thread(a1);
a1.setNumber(10);
threadA.setName("add1");
threadB.setName("add2");
threadC=new Thread(a2);
threadD=new Thread(a2);
a2.setNumber(-10);
threadC.setName("sub1");
threadD.setName("sub2");
threadA.start();
threadB.start();
threadC.start();
threadD.start();
}
}
class TargetObject implements Runnable {
private int number=0;
public void setNumber(int n) {
number=n;
}
public void run() {
while (true) {
if (Thread.currentThread().getName().substring(0,3).equals("add")) {
number++;
System.out.println("现在是"+Thread.currentThread().getName()+"在运行 number等于"+number);
}
if (Thread.currentThread().getName().substring(0,3).equals("sub")) {
number--;
System.out.println("现在是"+Thread.currentThread().getName()+"在运行 number等于"+number);
}
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("there is interruption!"+e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -