查看本章节
查看作业目录
需求说明:
模拟网上商城购物,当用户选好物品提交订单时,每笔订单会自动生成一个唯一的订单编号。而部分电子商务网站在数据高峰期时,一毫秒可能需要处理近千笔的订单
现在简单模拟 5 个订单同时提交,生成唯一订单编号的场景。订单编号的生成规则是以毫秒为单位的当前时间+订单序号
例如,当前时间的毫秒数是 150000,那第 1 笔订单的编号就是 1500001,第 9 笔订单的编号就是 1500009
实现思路:
项目中创建订单类 Order(订单编号、订单金额、下单用户)
创建订单处理类 ProcessOrder实现Runnable接口,定义Order类型的成员变量和一个Integer静态count作为订单序号(初始1),同时添加产生订单号的方法
产生订单号方法中使用sleep延迟500毫秒,同时添加同步代码块,指定静态变量 count 为同步监视器,同步代码块内则采用系统时间毫秒和订单序号生成订单号操作
在同步代码块外,把订单用户、订单金额和订单编号打印输出
创建测试启动类 TestOrder。在该类的 main() 方法内,使用有参构造方法实例化 5 个 Order 对象。使用ProcessOrder(Order order) 构造方法,实例化 5 个 ProcessOrder 类的对象,并把 5 个 Order 对象依次传给构造方法的参数 order。分别创建5个Thread对象依次传入5个ProcessOrder,启动测试
实现代码:
订单类(Order)
public class Order {
//订单编号
private String orderNumber;
//订单金额
private double orderAmount;
//用户名称
private String username;
//无参构造
public Order() {
super();
}
//有参构造
public Order(String orderNumber, double orderAmount, String username) {
super();
this.orderNumber = orderNumber;
this.orderAmount = orderAmount;
this.username = username;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public double getOrderAmount() {
return orderAmount;
}
public void setOrderAmount(double orderAmount) {
this.orderAmount = orderAmount;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
订单处理类(ProcessOrder)
public class ProcessOrder implements Runnable{
private Order order;
//订单计数器
private static Integer count = 1;
public ProcessOrder(Order order) {
this.order = order;
}
@Override
public void run() {
generateOrderNumber();
}
public void generateOrderNumber() {
try {
//加入线程延迟
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//同步监听器是count变量
synchronized (count) {
//生成订单编号
StringBuffer orderNumber = new StringBuffer("").append(System.currentTimeMillis()).append(count++);
//将订单编号保存到订单对象中
order.setOrderNumber(orderNumber.toString());
}
System.out.printf("%s的订单金额是%.2f元,订单编号是:%s\n",order.getUsername(),order.getOrderAmount(),order.getOrderNumber());
}
}
启动运行类(TestOrder)
public class TestOrder {
public static void main(String[] args) {
//实例化5个订单对象
Order order1 = new Order(null,300,"张三");
Order order2 = new Order(null,400,"李四");
Order order3 = new Order(null,500,"王五");
Order order4 = new Order(null,300,"赵六");
Order order5 = new Order(null,200,"秦七");
//实例化5个Runnable对象
ProcessOrder processOrder1 = new ProcessOrder(order1);
ProcessOrder processOrder2 = new ProcessOrder(order2);
ProcessOrder processOrder3 = new ProcessOrder(order3);
ProcessOrder processOrder4 = new ProcessOrder(order4);
ProcessOrder processOrder5 = new ProcessOrder(order5);
//实例化5条线程
Thread thread1 = new Thread(processOrder1);
Thread thread2 = new Thread(processOrder2);
Thread thread3 = new Thread(processOrder3);
Thread thread4 = new Thread(processOrder4);
Thread thread5 = new Thread(processOrder5);
//依次启动线程
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
}
}