ID生成服务

ID生成服务本身应该串行化,避免锁竞争.
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.util.concurrent.Callable;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.Future;

  10. public class SeqGenerator {
  11.     private static ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

  12.     private static int currentVal = -1;
  13.     private static int maxVal = -1;
  14.     private static int fetchSize = 10000;

  15.     static{
  16.         try {
  17.             fetchFromDB();
  18.         } catch (Exception e) {
  19.             // TODO Auto-generated catch block
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.     
  24.     public static int getSeq() throws InterruptedException, ExecutionException {
  25.         Callable<Integer> c = new Callable<Integer>() {
  26.             @Override
  27.             public Integer call() throws Exception {
  28.                 int result = currentVal;
  29.                 if (currentVal > maxVal) {
  30.                     fetchFromDB();
  31.                     result = currentVal;
  32.                 }
  33.                 currentVal++;
  34.                 return result;

  35.             }
  36.         };
  37.         Future<Integer> task = singleThreadExecutor.submit(c);
  38.         return task.get().intValue();
  39.     }

  40.     private static void fetchFromDB() throws Exception {
  41.         Class.forName("com.mysql.jdbc.Driver");
  42.         Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "xx", "xx");
  43.         connection.setAutoCommit(false);
  44.         Statement st = connection.createStatement();
  45.         ResultSet rs = st.executeQuery("select * from test for update");
  46.         rs.next();
  47.         currentVal = rs.getInt(1) + 1;
  48.         rs.close();
  49.         st.executeUpdate("update test set id=id+" + fetchSize);
  50.         rs = st.executeQuery("select * from test for update");
  51.         rs.next();
  52.         maxVal = rs.getInt(1);
  53.         connection.commit();
  54.         rs.close();
  55.         st.close();
  56.         connection.close();
  57.     }

  58.     public static void main(String[] args) throws Exception {
  59.         int i = 1000000;
  60.         long start = System.currentTimeMillis();

  61.         while (i > 0) {
  62.             System.out.println(SeqGenerator.getSeq());
  63.             i--;
  64.         }
  65.         long end = System.currentTimeMillis();
  66.         System.out.println(end - start);
  67.     }
  68. }
    使用这种自定义序列,1百万ID的生成时间是14s.效果非常明显.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值