public void test1ByOriginal() throws InterruptedException { final CountDownLatch beginLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT); for (int i = 0; i < THREAD_COUNT; ++i) { Thread thread = new Thread() { public void run() { try { beginLatch.await(); for (int i = 0; i < RUN_COUNT; ++i) { DBObject dbo = new BasicDBObject(); dbo.put("sid", i); dbo.put("name", "学员" + i); dbo.put("age", R.random(10, 16)); dbo.put("birthday", new Date()); dbo.put("address", "abcdefg"); coll.insert(dbo); } } catch (Exception e) { e.printStackTrace(); } finally { endLatch.countDown(); } } }; thread.start(); } Stopwatch sw = Stopwatch.begin(); beginLatch.countDown(); endLatch.await(); sw.stop(); System.out.printf( "原生驱动\t多线程插入 %d * %d 条,耗时%8dms,平均%8d条/秒\n", THREAD_COUNT, RUN_COUNT, sw.getDuration(), THREAD_COUNT * RUN_COUNT * 1000 / sw.getDuration()); }
public void test1ByNutz() throws InterruptedException { final CountDownLatch beginLatch = new CountDownLatch(1); final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT); for (int i = 0; i < THREAD_COUNT; ++i) { Thread thread = new Thread() { public void run() { try { beginLatch.await(); for (int i = 0; i < RUN_COUNT; ++i) { Student s = new Student(); s.setSid(i); s.setName("学员" + i); s.setAge(R.random(10, 16)); s.setBirthday(new Date()); s.setAddress("abcdefg"); dao.save(s); } } catch (Exception e) { e.printStackTrace(); } finally { endLatch.countDown(); } } }; thread.start(); } Stopwatch sw = Stopwatch.begin(); beginLatch.countDown(); endLatch.await(); sw.stop(); System.out.printf( "Nutz\t多线程插入 %d * %d 条,耗时%8dms,平均%8d条/秒\n", THREAD_COUNT, RUN_COUNT, sw.getDuration(), THREAD_COUNT * RUN_COUNT * 1000 / sw.getDuration()); }