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

📄 例五.txt

📁 这是一本java基础教程 对新手上路有很大帮助
💻 TXT
字号:
class TargetObject implements Runnable{
    private int number=0;
    public void setNumber(int n){
        number=n;
    }
    public void run(){
        while(true){
           if(Thread.currentThread().getName().equals("add")){
               number++;
               System.out.printf("%d\n",number);
           }
           if(Thread.currentThread().getName().equals("sub")){
               number--;
               System.out.printf("%12d\n",number);
           }
          try{  Thread.sleep(1000);
          }
          catch(InterruptedException e) {}
        } 
    }
}
public class Example8_5{
    public static void main(String args[ ]){
        Thread threadA,threadB,threadC,threadD;
        TargetObject a1=new TargetObject(),            //线程的目标对象
                   a2=new TargetObject();
        threadA=new Thread(a1);                    //目标对象是a1的线程
        threadB=new Thread(a1);
        a1.setNumber(10);
        threadA.setName("add");
        threadB.setName("add");
        threadC=new Thread(a2);                    //目标对象是a2的线程
        threadD=new Thread(a2);
        a2.setNumber(-10);
        threadC.setName("sub");
        threadD.setName("sub");
        threadA.start();
        threadB.start();
        threadC.start();
        threadD.start();
    }
}

⌨️ 快捷键说明

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