多线程性能测试demo

@Autowired
	private DataStoreComponent dBComponent;

	@Test
	public void MyTest() throws Exception {
		final CountDownLatch begin = new CountDownLatch(1);
		final ExecutorService exec = Executors.newFixedThreadPool(100);
		for (int i = 0; i < 1000; i++) {
			Runnable runnable = new Runnable() {
				public void run() {
					try {
						begin.await();
						String result = getResult().toString();
						log.info("----------- " + result);
					} catch (InterruptedException e) {
						e.printStackTrace();
						log.error("InterruptedException : {}", e.getMessage());
					} catch (Exception e1) {
						log.error(Thread.currentThread().getName() + " -- " + e1.getMessage());
					}
				}
			};
			exec.submit(runnable);
		}
		System.out.println("开始执行");
		begin.countDown();
		exec.awaitTermination(4000, TimeUnit.MILLISECONDS);

		//加入main方法抢占资源
		for (int i = 0; i < 10; i++) {
			try {
				String result = getResult().toString();
				log.info("*********" + result);
			} catch (Exception e1) {
				log.error(Thread.currentThread().getName() + " -- " + e1.getMessage());
			}
		}

	}

	private R getResult() throws IOException {
		R info = new R();
		Set<String> set = new HashSet<String>();
		if (null != dBComponent.getDataStore()) {
			SimpleFeatureSource source = dBComponent.getDataStore().getFeatureSource("JG_JOBPT");
			FeatureType ft = source.getSchema();
			Collection<PropertyDescriptor> fileds = ft.getDescriptors();
			for (PropertyDescriptor f : fileds) {
				String n = f.getName().toString();
				set.add(n);
			}
			info.setData(set);
		}
		return info;
	}


猜你喜欢

转载自blog.csdn.net/mubin_/article/details/80916176