Back to Repositories

Testing Custom ThreadPool Future Implementation in JCSprout

This test suite evaluates a custom thread pool implementation with Future functionality in Java. It validates concurrent task execution, thread management, and asynchronous result handling through a custom Future interface implementation.

Test Coverage Overview

The test suite covers essential aspects of concurrent programming and thread pool management:

  • Thread pool initialization and configuration
  • Task submission and execution
  • Future-based result handling
  • Shutdown behavior and completion notification
  • Worker task implementation and state management

Implementation Analysis

The testing approach utilizes JUnit framework to verify custom thread pool behavior with Future pattern implementation. The test validates blocking queue operations, task execution flow, and asynchronous result retrieval through Future objects.

Key patterns include worker thread management, task queuing, and completion notification callbacks.

Technical Details

  • JUnit test framework
  • SLF4J logging implementation
  • ArrayBlockingQueue for task management
  • Custom Future interface implementation
  • TimeUnit-based execution delays
  • Notify interface for completion callbacks

Best Practices Demonstrated

The test demonstrates robust concurrent programming practices including proper resource management, clear separation of concerns, and effective error handling.

  • Structured thread pool configuration
  • Proper shutdown handling
  • Async task result management
  • Clean worker task implementation

crossoverjie/jcsprout

src/test/java/com/crossoverjie/concurrent/CustomThreadPoolFutureTest.java

            
package com.crossoverjie.concurrent;

import com.crossoverjie.concurrent.communication.Notify;
import com.crossoverjie.concurrent.future.Callable;
import com.crossoverjie.concurrent.future.Future;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class CustomThreadPoolFutureTest {
    private final static Logger LOGGER = LoggerFactory.getLogger(CustomThreadPoolFutureTest.class);
    @Test
    public void execute() {
    }


    public static void main(String[] args) throws InterruptedException {
        BlockingQueue queue = new ArrayBlockingQueue<>(10);
        CustomThreadPool pool = new CustomThreadPool(3, 5, 1, TimeUnit.SECONDS, queue, new Notify() {
            @Override
            public void notifyListen() {
                LOGGER.info("任务执行完毕");
            }
        }) ;

        List<Future> futures = new ArrayList<>() ;
        for (int i = 0; i < 10; i++) {
            Future<Integer> future = pool.submit(new Worker(i));
            futures.add(future) ;
        }

        pool.shutdown();
        LOGGER.info("++++++++++++++");
        pool.mainNotify();
        for (Future<Integer> future : futures) {
            Integer integer = future.get();
            LOGGER.info("future======{}" ,integer);
        }




    }




    private static class Worker implements Callable<Integer> {

        private int state ;

        public Worker(int state) {
            this.state = state;
        }

        @Override
        public Integer call() {
            try {
                TimeUnit.SECONDS.sleep(1);
                LOGGER.info("state={}",state);
                return state + 1 ;
            } catch (InterruptedException e) {

            }

            return 0 ;
        }
    }
}