多线程并发环境下的压测代码
- String resource = "mybatis-config.xml";
- InputStream in = null;
- try {
- in = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
- final List<Long> ids = Collections.singletonList(1L);
- final SqlSession session = sqlSessionFactory.openSession();
- final CountDownLatch mCountDownLatch = new CountDownLatch(1);
- for (int i = 0; i < 50; i++) {
- Thread thread = new Thread(new Runnable() {
- public void run() {
- try {
- mCountDownLatch.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- for (int k = 0; k < 100; k++) {
- session.selectList("CompanyMapper.getCompanysByIds", ids);
- }
- }
- });
- thread.start();
- }
- mCountDownLatch.countDown();
- synchronized (MybatisBugTest.class) {
- try {
- MybatisBugTest.class.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Throwable e) {
- e.printStackTrace();
- } finally {
- if (in != null)
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
(编辑:西安站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|