java中继承Thread类的实际编码应用

1,定义一个类例如创建订单类:CreateOrderThread  extends  Thread 类;

2,类中定义成员变量 : private String productId;

     private Integer  price;

     private  Integer  num;

     注:如果需要biz接口在这里进行方法调用时,也把biz 定义在成员变量位置;

    private createOrderBiz createOrderbiz;

3,创建该类的构造方法,其中的参数为成员变量中的某些变量:

  public  createOrderThread( String productId, Integer  price, Integer num, createOrderBiz createOrderbiz){

  this.productId = productId;

  this.price  = price ;

  this.num  = num;

  this.createOrderbiz = createOrderbiz;

 }

4,重写run方法

public void  run(){

  createOrderbiz.createOrder(productId,num,price);

}

5.在程序入口:controller 里,收集到必要的参数后: 

 CreateOrderThread   c = new CreateOrderThread(productId,price,num,createOrderbiz);

   Thread t = new Thread(c);
    t.start();

当然其中的异常情况,及返回的信息可以在thread里面定义;

这里继承Thread类的子类中可以定义n多个成员变量, 以及n多个构造方法(当然成员变量一定要满足构造方法中参数),来适应各种不同的情况;

各位读者可以自由发挥,本文只仅作为一个参考;

猜你喜欢

转载自blog.csdn.net/q18810146167/article/details/62229183
今日推荐