丐帮洪七公和郭靖在路上碰到了黄老邪和老毒物

模拟:
丐帮洪七公和郭靖在路上碰到了黄老邪和老毒物。
洪七公用降龙十八掌对付黄老邪
郭靖也用降龙十八掌对付老毒物

提醒:不共享数据,但共享代码

 

public class Exce4Demo {
 /**
  * 
  * 只是共享代码,不共享数据,不必加锁
  * @param args
  */

 public static void main(String[] args){
  
  User user = new User("黄老邪");
  User user2 = new User("老毒物");
  
  Thread fight = new Fight(user,"洪七公");
  Thread fight2 = new Fight(user2,"郭靖");
  
  fight.start();
  fight2.start();
 }
}


class User{
 
 private String name;
 
 public User(String name){
  
  this.name = name;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 
}

class Fight extends Thread{
 
 private User user;
 
 public Fight(User user,String name){
  
  super(name);
  this.user = user;
 }
 
 public void run(){
  
  String name = Thread.currentThread().getName();
  for(int i = 0; i < 10; i++ ){
   
   System.out.println(name+"用降龙十八掌对付"+this.user.getName());
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
  System.out.println(name+"打败了"+this.user.getName());
 }
 
}
 

 

你可能感兴趣的:(在路上)