앱)자바 멀티스레드 샘플 코드
<MAIN>
public static void main(String[] args){
SetMultiQue setMultiQue = new SetMultiQue();
setMultiQue.execute();
}
<GroupThread>
public class GroupThread implements Runnable{
private List<RequestInfoVO> requestInfo = new ArrayList<RequestInfoVO>();
public List<RequestInfoVO> getRequestInfo(){
return requestInfo;
}
public void setRequestInfo(List<RequestInfoVO> requestInfo) {
this.requestInfo = requestInfo;
}
public void addRequestInfo(RequestInfoVO requestInfoVO){
requestInfo.add(requestInfoVO);
}
@Override
public void run(){
sysout("Thread " + Thread.currentThread().getName()+" Start");
sysout("Thread " + Thread.currentThread().getName()+" End");
}
}
<RequestInfoVO>
public class RequestInfoVO {
private String requestID;
private String name;
}
<SetMultiQue>
public class SetMultiQue {
private final int BATCH_CNT=100;
private final int BATCH_TIMEOUT_HOUR = 3;
public void execute(){
List<TaskThread> taskThreadList = new ArrayList<TaskThread>();
List<RequestInfoVO> requestInfoList = getRequestInfo();
GroupThread groupThread= null;
for(int i=0; i<requestInfoList.size(); i++) {
RequestInfoVO reqInfo = RequestInfoList.get(i);
if(groupThread == null){
groupThread= new GroupThread();
}
groupThread.addRequestInfo(reqInfo);
if(i != requestInfoList.size()-1){
taskThreadList.add(groupThread);
groupThread = new GroupThread();
}
}
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(BATCH_CNT, BATCH_CNT, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
for(int i=0; i<taskThreadList.size(); i++){
threadPool.execute(taskThreadList.get(i));
}
//BATCH_TIMEOUT_HOUR 시간만큼 기다렸다가 종료
threadPool.shutdown();
threadPool.awitTermination(BATCH_TIMEOUT_HOUR, TimeUnit.HOURS);
}
public List<RequestInfoVO> getRequestInfo() {
//DB에서 한번에 처리할수 있는 양의 정보 가져옴
//파싱해서 객체에 저장
RequestInfoVO info = null;
List<RequestInfoVO> requestInfoList = new ArraryList<RequestInfoVO>();
Random random = new Random(10);
for (int i=0; i<100; i++){
int test_index = random.nextInt(60)+1;
info = new RequestInfoVO();
info.setRequestId(i+1+"");
info.setName("Name"+test_index);
requestInfoList.add(info);
}
Collections.sort(requestInfoList, new CompareNameAsc());
return requestInfoList;
}
static class CompareNameAsc implements Comparator<RequestInfoVO> {
@Override
public int compare(RequestInfoVO o1, RequestIfnoVO o2) {
return o1.getName().compareTo(o2.getName());
}
}
}
public static void main(String[] args){
SetMultiQue setMultiQue = new SetMultiQue();
setMultiQue.execute();
}
<GroupThread>
public class GroupThread implements Runnable{
private List<RequestInfoVO> requestInfo = new ArrayList<RequestInfoVO>();
public List<RequestInfoVO> getRequestInfo(){
return requestInfo;
}
public void setRequestInfo(List<RequestInfoVO> requestInfo) {
this.requestInfo = requestInfo;
}
public void addRequestInfo(RequestInfoVO requestInfoVO){
requestInfo.add(requestInfoVO);
}
@Override
public void run(){
sysout("Thread " + Thread.currentThread().getName()+" Start");
sysout("Thread " + Thread.currentThread().getName()+" End");
}
}
<RequestInfoVO>
public class RequestInfoVO {
private String requestID;
private String name;
}
<SetMultiQue>
public class SetMultiQue {
private final int BATCH_CNT=100;
private final int BATCH_TIMEOUT_HOUR = 3;
public void execute(){
List<TaskThread> taskThreadList = new ArrayList<TaskThread>();
List<RequestInfoVO> requestInfoList = getRequestInfo();
GroupThread groupThread= null;
for(int i=0; i<requestInfoList.size(); i++) {
RequestInfoVO reqInfo = RequestInfoList.get(i);
if(groupThread == null){
groupThread= new GroupThread();
}
groupThread.addRequestInfo(reqInfo);
if(i != requestInfoList.size()-1){
taskThreadList.add(groupThread);
groupThread = new GroupThread();
}
}
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(BATCH_CNT, BATCH_CNT, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
for(int i=0; i<taskThreadList.size(); i++){
threadPool.execute(taskThreadList.get(i));
}
//BATCH_TIMEOUT_HOUR 시간만큼 기다렸다가 종료
threadPool.shutdown();
threadPool.awitTermination(BATCH_TIMEOUT_HOUR, TimeUnit.HOURS);
}
public List<RequestInfoVO> getRequestInfo() {
//DB에서 한번에 처리할수 있는 양의 정보 가져옴
//파싱해서 객체에 저장
RequestInfoVO info = null;
List<RequestInfoVO> requestInfoList = new ArraryList<RequestInfoVO>();
Random random = new Random(10);
for (int i=0; i<100; i++){
int test_index = random.nextInt(60)+1;
info = new RequestInfoVO();
info.setRequestId(i+1+"");
info.setName("Name"+test_index);
requestInfoList.add(info);
}
Collections.sort(requestInfoList, new CompareNameAsc());
return requestInfoList;
}
static class CompareNameAsc implements Comparator<RequestInfoVO> {
@Override
public int compare(RequestInfoVO o1, RequestIfnoVO o2) {
return o1.getName().compareTo(o2.getName());
}
}
}
'백엔드개발 > 자바스프링' 카테고리의 다른 글
앱)spring transaction properties mybatis Config설정 예시 샘플 (0) | 2018.11.08 |
---|---|
앱)자바 enum 예제 (0) | 2018.07.09 |
앱)JSP(JavaServer Pages)에 대해서 (0) | 2018.07.03 |
앱)AWS SES Email Util java spring 예제 샘플 (0) | 2018.06.27 |
앱)jstl list search result table sample 예제 (0) | 2018.06.12 |