/**
* 2017年11月13日下午7:14:42
aop-uc-internet
*/
package com.casic.test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.casic.tenant.model.TenantInfo;
import com.hotent.core.util.UniqueIdUtil;
import com.hotent.platform.model.system.SysOrgInfo;
import com.hotent.platform.service.system.SysOrgInfoService;
import com.hotent.platform.service.system.SysUserService;
/**
* @author Administrator
*TestThread$
*/
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations={"classpath*:conf/app-context.xml"}) //加载配置文件
public class TestThread {
@Resource
private SysUserService sysUserService;
@Test
public void test1(){
//ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
ArrayList<Future<Resources>> FutureList = new ArrayList<Future<Resources>>();
for ( int j = 0; j < 33000; j++) {
Future<Resources> future = fixedThreadPool.submit(new Callable<Resources>() {
@Override
public Resources call() throws Exception {
long currentTimeMillis = System.currentTimeMillis();
String name = Thread.currentThread().getName();
System.out.println("开始时间:"+currentTimeMillis+"---"+name+"---开始工作了。。。。。。。。。。。。。。。。。。");
synchronized (TestThread.class) {
TenantInfo sysOrgInfo=new TenantInfo();
Random random=new Random();
int i= random.nextInt(20000000);
sysOrgInfo.setSysOrgInfoId(Long.parseLong("740117"+13+i));
sysOrgInfo.setName("11王树民企业");
sysUserService.registerSysOrgAndUser(sysOrgInfo, "1223");
}
Resources resource2 = new Resources();
resource2.setResourceName("结束时间:"+currentTimeMillis+""+name+"---结束工作了。。。。。。。完成的任务号:{ }"+resource2.getTotal()+"。。。。。。。。");
return resource2;
}
});
FutureList.add(future);
}
for (Future<Resources> future2 : FutureList) {
if(true){
try {
Resources resource = future2.get();
boolean done = future2.isDone();
System.out.println("done:"+done);
System.out.println("返回結果"+resource.getResourceName());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
}
class Resources implements Serializable{
private String ResourceName;
private int total;
/**
* @return the resourceName
*/
protected String getResourceName() {
return ResourceName;
}
/**
* @param resourceName the resourceName to set
*/
protected void setResourceName(String resourceName) {
ResourceName = resourceName;
}
/**
* @return the total
*/
protected int getTotal() {
return total;
}
/**
* @param total the total to set
*/
protected void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return "Resource [ResourceName=" + ResourceName + ", total=" + total
+ "]";
}
/**
* @param resourceName
* @param total
*/
public Resources(String resourceName, int total) {
super();
ResourceName = resourceName;
this.total = total;
}
/**
*
*/
public Resources() {
// TODO Auto-generated constructor stub
}
}
* 2017年11月13日下午7:14:42
aop-uc-internet
*/
package com.casic.test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.casic.tenant.model.TenantInfo;
import com.hotent.core.util.UniqueIdUtil;
import com.hotent.platform.model.system.SysOrgInfo;
import com.hotent.platform.service.system.SysOrgInfoService;
import com.hotent.platform.service.system.SysUserService;
/**
* @author Administrator
*TestThread$
*/
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations={"classpath*:conf/app-context.xml"}) //加载配置文件
public class TestThread {
@Resource
private SysUserService sysUserService;
@Test
public void test1(){
//ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
ArrayList<Future<Resources>> FutureList = new ArrayList<Future<Resources>>();
for ( int j = 0; j < 33000; j++) {
Future<Resources> future = fixedThreadPool.submit(new Callable<Resources>() {
@Override
public Resources call() throws Exception {
long currentTimeMillis = System.currentTimeMillis();
String name = Thread.currentThread().getName();
System.out.println("开始时间:"+currentTimeMillis+"---"+name+"---开始工作了。。。。。。。。。。。。。。。。。。");
synchronized (TestThread.class) {
TenantInfo sysOrgInfo=new TenantInfo();
Random random=new Random();
int i= random.nextInt(20000000);
sysOrgInfo.setSysOrgInfoId(Long.parseLong("740117"+13+i));
sysOrgInfo.setName("11王树民企业");
sysUserService.registerSysOrgAndUser(sysOrgInfo, "1223");
}
Resources resource2 = new Resources();
resource2.setResourceName("结束时间:"+currentTimeMillis+""+name+"---结束工作了。。。。。。。完成的任务号:{ }"+resource2.getTotal()+"。。。。。。。。");
return resource2;
}
});
FutureList.add(future);
}
for (Future<Resources> future2 : FutureList) {
if(true){
try {
Resources resource = future2.get();
boolean done = future2.isDone();
System.out.println("done:"+done);
System.out.println("返回結果"+resource.getResourceName());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
}
class Resources implements Serializable{
private String ResourceName;
private int total;
/**
* @return the resourceName
*/
protected String getResourceName() {
return ResourceName;
}
/**
* @param resourceName the resourceName to set
*/
protected void setResourceName(String resourceName) {
ResourceName = resourceName;
}
/**
* @return the total
*/
protected int getTotal() {
return total;
}
/**
* @param total the total to set
*/
protected void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return "Resource [ResourceName=" + ResourceName + ", total=" + total
+ "]";
}
/**
* @param resourceName
* @param total
*/
public Resources(String resourceName, int total) {
super();
ResourceName = resourceName;
this.total = total;
}
/**
*
*/
public Resources() {
// TODO Auto-generated constructor stub
}
}