> Java > java지도 시간 > 본문

Java 다중 스레드 처리 작업 캡슐화

伊谢尔伦
풀어 주다: 2016-12-05 11:16:02
원래의
1221명이 탐색했습니다.

최근 프로젝트의 많은 부분에서 일부 작업을 처리하기 위해 멀티스레딩을 사용하고 있으며, 로직 코드와 Java 멀티스레딩 처리 코드가 혼합되어 있어 코드 관련 처리가 매우 열악합니다. Java 멀티스레딩이 추출되고 코드의 측면이 반복적으로 사용됩니다. 좋지 않으니 누구나 사용해 보세요

사용 방법은 매우 간단합니다. 사용 방법은 두 가지가 있습니다

1. 일괄 작업을 멀티 스레드에 직접 전달합니다.

코드는 다음과 같습니다.

/**
 * Created with IntelliJ IDEA.
 * 测试多线程处理任务
 * className: TaskMulThreadServiceTest
 *
 * @version 1.0
 *          Date Time: a
 *@author: ddys
 */public class TaskMulThreadServiceTest extends TestCase implements ITaskHandle<String,Boolean>{

    public void testExecute() throws Exception {
        String [] taskItems = new String[100];        
            for (int i=0;i<100;i++){
            taskItems[i]="任务"+i;
        }
        IMulThreadService<String,Boolean> mulThreadService = new TaskMulThreadService(this);
        long start = System.currentTimeMillis();
        List<Boolean> result = mulThreadService.execute(taskItems);        
               for (Boolean e : result){            
                   if(!e){
                System.out.println("任务处理失败");
            }
        }
        System.out.println("所有任务处理完成,耗时"+(System.currentTimeMillis()-start)+",任务成功数"+result.size());
    }    
      /**
     * Created with IntelliJ IDEA.
     * 执行任务,返回所有执行的结果
     * className: TaskMulThreadService
     *
     * @author: ddys
     * @version 1.0
     * Date Time:
     */
    public Boolean execute(String s) {
        System.out.println(Thread.currentThread().getId()+"线程正在处理"+s);        
                   return true;
    }
}
로그인 후 복사

2. 첨부된 쿼리 작업 방법은 이 쿼리 작업 방법과 비즈니스 처리 방법을 구현한 후 실행합니다. 처리 결과를 반환

코드는 다음과 같습니다.

ate Time: a
 *@author: XWK
 */
public class SelectTaskMulThreadServiceTest extends TestCase implements ISelectTask<String,Boolean>{
    public void testExecute() throws Exception {
        IMulThreadService<String,Boolean> mulThreadService = new SelectTaskMulThreadService(this);
        long start = System.currentTimeMillis();
        List<Boolean> result = mulThreadService.execute();        
        for (Boolean e : result){            
        if(!e){
                System.out.println("任务处理失败");
            }
        }
        System.out.println("所有任务处理完成,耗时"+(System.currentTimeMillis()-start)+",任务成功数"+result.size());
    }    
    /**
     * Created with IntelliJ IDEA.
     * 执行任务,返回所有执行的结果
     * className: TaskMulThreadService
     *
     * @author: ddys
     * @version 1.0
     * Date Time:
     */
    public Boolean execute(String s) {
        System.out.println(Thread.currentThread().getId()+"线程正在处理"+s);        
        return true;
    }    /**
     * @param &#39;a 传递参数
     * @return a 回类型
     * @throws
     * @Title: a
     * @Description: 获取一批任务
     * @author ddys
     * @date 2015-11-15 21:09
     */
    public String[] getTaskItem() {
        String [] taskItems = new String[100];        
        for (int i=0;i<100;i++){
            taskItems[i]="任务"+i;
        }        
        return taskItems;
    }
}
로그인 후 복사


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!