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

📄 threadexp3two.java

📁 java关于多线程的课堂练习
💻 JAVA
字号:
/**
 * @(#)ThreadExp3Two.java
 *
 *
 * @author 
 * @version 1.00 2007/12/3
 */
public class ThreadExp3Two {
        
    /**
     * Creates a new instance of <code>ThreadExp3</code>.
     */
    public ThreadExp3Two() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       Thread threadA,threadB;
       TargetObject  a=new TargetObject();
       threadA=new Thread(a);
       threadB=new Thread(a);
      
       threadA.setName("add1");
       threadB.setName("sub1");
       
       threadA.start();
       threadB.start();
       
    }
}
class  TargetObject  implements  Runnable {
 
 
 public void run() {
  int  number=0;
  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 + -