Rumah >Java >javaTutorial >Bagaimana untuk melaksanakan program berbilang benang menggunakan Java?

Bagaimana untuk melaksanakan program berbilang benang menggunakan Java?

PHPz
PHPzke hadapan
2023-04-23 09:19:061380semak imbas

1. Kelas tugas pelaksanaan berbilang benang

package com.visy.threadpool;

import com.visy.executor.ExecutorFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ThreadPoolConfig {
    private TheadPoolProperties theadPoolProperties;
    private ThreadPoolExecutor executor;
    private ThreadPoolExecutor executorChild;

    public ThreadPoolConfig(TheadPoolProperties theadPoolProperties) {
        this.theadPoolProperties = theadPoolProperties;
        this.executor = ExecutorFactory.getInstance().getThreadPoolExecutor("ThreadPoolConfig-service", theadPoolProperties.getQueueSize(), theadPoolProperties.getCoreThreadNum(), theadPoolProperties.getMaxPoolSize());
        this.executorChild = ExecutorFactory.getInstance().getThreadPoolExecutor("ThreadPoolConfig-service-child", theadPoolProperties.getQueueSize(), theadPoolProperties.getCoreThreadNum(), theadPoolProperties.getMaxPoolSize());
    }

    public <V> List<V> doConcurrentTask(List<Callable<V>> taskList, ThreadPoolExecutor... executorChilds) {
        if (taskList != null && !taskList.isEmpty()) {
            List<V> resultList = new ArrayList();
            List futureList = null;

            try {
                if (this.executor.getQueue().size() >= this.theadPoolProperties.getQueueSize()) {
                    throw new RuntimeException("queue size bigger than 100, now size is " + this.executor.getQueue().size());
                }

                if (executorChilds != null && executorChilds.length > 0 && executorChilds[0] != null) {
                    futureList = executorChilds[0].invokeAll(taskList);
                } else {
                    futureList = this.executor.invokeAll(taskList, (long)this.theadPoolProperties.getTimeOut(), TimeUnit.SECONDS);
                }
            } catch (InterruptedException var6) {
                var6.printStackTrace();
            }

            this.doFutureList(resultList, futureList);
            return resultList;
        } else {
            return null;
        }
    }

    <V> void doFutureList(List<V> resultList, List<Future<V>> futureList) {
        if (futureList != null) {
            Iterator var3 = futureList.iterator();

            while(var3.hasNext()) {
                Future future = (Future)var3.next();

                try {
                    resultList.add(future.get());
                } catch (ExecutionException | InterruptedException var6) {
                    var6.printStackTrace();
                }
            }
        }

    }

    public <V> void doVoidConcurrentTask(List<Callable<V>> taskList) {
        if (taskList != null && !taskList.isEmpty()) {
            Iterator var2 = taskList.iterator();

            while(var2.hasNext()) {
                Callable<V> call = (Callable)var2.next();
                this.executor.submit(call);
            }

        }
    }

    public TheadPoolProperties getTheadPoolProperties() {
        return this.theadPoolProperties;
    }

    public ThreadPoolExecutor getExecutor() {
        return this.executor;
    }

    public ThreadPoolExecutor getExecutorChild() {
        return this.executorChild;
    }

    public void setTheadPoolProperties(TheadPoolProperties theadPoolProperties) {
        this.theadPoolProperties = theadPoolProperties;
    }

    public void setExecutor(ThreadPoolExecutor executor) {
        this.executor = executor;
    }

    public void setExecutorChild(ThreadPoolExecutor executorChild) {
        this.executorChild = executorChild;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof ThreadPoolConfig)) {
            return false;
        } else {
            ThreadPoolConfig other = (ThreadPoolConfig)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    Object this$theadPoolProperties = this.getTheadPoolProperties();
                    Object other$theadPoolProperties = other.getTheadPoolProperties();
                    if (this$theadPoolProperties == null) {
                        if (other$theadPoolProperties == null) {
                            break label47;
                        }
                    } else if (this$theadPoolProperties.equals(other$theadPoolProperties)) {
                        break label47;
                    }

                    return false;
                }

                Object this$executor = this.getExecutor();
                Object other$executor = other.getExecutor();
                if (this$executor == null) {
                    if (other$executor != null) {
                        return false;
                    }
                } else if (!this$executor.equals(other$executor)) {
                    return false;
                }

                Object this$executorChild = this.getExecutorChild();
                Object other$executorChild = other.getExecutorChild();
                if (this$executorChild == null) {
                    if (other$executorChild != null) {
                        return false;
                    }
                } else if (!this$executorChild.equals(other$executorChild)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof ThreadPoolConfig;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $theadPoolProperties = this.getTheadPoolProperties();
        int result = result * 59 + ($theadPoolProperties == null ? 43 : $theadPoolProperties.hashCode());
        Object $executor = this.getExecutor();
        result = result * 59 + ($executor == null ? 43 : $executor.hashCode());
        Object $executorChild = this.getExecutorChild();
        result = result * 59 + ($executorChild == null ? 43 : $executorChild.hashCode());
        return result;
    }

    public String toString() {
        return "ThreadPoolConfig(theadPoolProperties=" + this.getTheadPoolProperties() + ", executor=" + this.getExecutor() + ", executorChild=" + this.getExecutorChild() + ")";
    }
}

2 4 .Senaraikan kelas alat pemisah

package com.visy.executor;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExecutorFactory {
    private static final Logger logger = LoggerFactory.getLogger(ExecutorFactory.class);
    private static final Map<String, ThreadPoolExecutor> threadPoolExecutorMap = new ConcurrentHashMap();
    private static final int DEFAULT_QUEUE_SIZE = 1000;
    private static final String DEFAULT_EXECUTOR_NAME = "default-executor";
    private static final int MAX_THREAD_NUM = 100;
    private static final int CORE_THREAD_NUM = 1;
    private static volatile ExecutorFactory instance;

    private ExecutorFactory() {
    }

    public static ExecutorFactory getInstance() {
        if (instance == null) {
            Class var0 = ExecutorFactory.class;
            synchronized(ExecutorFactory.class) {
                if (instance == null) {
                    instance = new ExecutorFactory();
                }
            }
        }

        return instance;
    }

    public ThreadPoolExecutor getThreadPoolExecutorByName(String name) {
        return (ThreadPoolExecutor)threadPoolExecutorMap.get(name);
    }

    public static Map<String, ThreadPoolExecutor> getThreadPoolExecutorMap() {
        return threadPoolExecutorMap;
    }

    public ThreadPoolExecutor getThreadPoolExecutor(String threadPoolExecutorName, int queueSize, int coreThreadNum, int maxPoolSize) {
        if (StringUtils.isBlank(threadPoolExecutorName)) {
            throw new IllegalArgumentException("thread name empty");
        } else {
            if (!threadPoolExecutorMap.containsKey(threadPoolExecutorName)) {
                Class var5 = ExecutorFactory.class;
                synchronized(ExecutorFactory.class) {
                    if (!threadPoolExecutorMap.containsKey(threadPoolExecutorName)) {
                        ThreadPoolExecutor executor = (new ThreadPool(coreThreadNum, maxPoolSize, 30L, queueSize, threadPoolExecutorName)).getExecutor();
                        threadPoolExecutorMap.put(threadPoolExecutorName, executor);
                        logger.info("thread name: {} executor created", threadPoolExecutorName);
                    }
                }
            }

            return (ThreadPoolExecutor)threadPoolExecutorMap.get(threadPoolExecutorName);
        }
    }

    public <T extends Runnable> void submit(T t) {
        ThreadPoolExecutor defaultExecutor = this.getThreadPoolExecutor();
        defaultExecutor.submit(t);
    }

    public <T extends Runnable> void submit(String poolName, T t) {
        ThreadPoolExecutor executor = this.getThreadPoolExecutorByName(poolName);
        if (executor == null) {
            logger.error("thread name: {} executor not exist.", poolName);
            throw new IllegalArgumentException("thread name:" + poolName + " executor not exist.");
        } else {
            executor.submit(t);
        }
    }

    public <T extends Callable<Object>> Future<Object> submit(T t) {
        ThreadPoolExecutor defaultExecutor = this.getThreadPoolExecutor();
        return defaultExecutor.submit(t);
    }

    public <T extends Callable<Object>> Future<Object> submit(String poolName, T t) {
        ThreadPoolExecutor executor = this.getThreadPoolExecutorByName(poolName);
        if (executor == null) {
            logger.error("thread poolName: {} executor not exist.", poolName);
            throw new IllegalArgumentException("thread poolName:" + poolName + " executor not exist.");
        } else {
            return executor.submit(t);
        }
    }

    public ThreadPoolExecutor getThreadPoolExecutor() {
        return this.getThreadPoolExecutor("default-executor", 1000, 1, 100);
    }
}

5. Kelas pembantu pelaksanaan pelbagai tugas

package com.visy.threadpool;

import javax.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

@Validated
@Configuration
@ConfigurationProperties(prefix = "visy.threadpool")
public class TheadPoolProperties {
    // 执行并行任务时,等待多久时间超时(单位:秒)
    @NotNull
    private Integer timeOut;
    // 队列大小
    @NotNull
    private Integer queueSize; 
    // 核心线程数量
    @NotNull
    private Integer coreThreadNum;
    // 线程池最大线程数量
    @NotNull
    private Integer maxPoolSize;
    // 并行执行每组大小
    private Integer groupSize = 20;

    public TheadPoolProperties() {
    }

    public Integer getTimeOut() {
        return this.timeOut;
    }

    public Integer getQueueSize() {
        return this.queueSize;
    }

    public Integer getCoreThreadNum() {
        return this.coreThreadNum;
    }

    public Integer getMaxPoolSize() {
        return this.maxPoolSize;
    }

    public Integer getGroupSize() {
        return this.groupSize;
    }

    public void setTimeOut(Integer timeOut) {
        this.timeOut = timeOut;
    }

    public void setQueueSize(Integer queueSize) {
        this.queueSize = queueSize;
    }

    public void setCoreThreadNum(Integer coreThreadNum) {
        this.coreThreadNum = coreThreadNum;
    }

    public void setMaxPoolSize(Integer maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public void setGroupSize(Integer groupSize) {
        this.groupSize = groupSize;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof TheadPoolProperties)) {
            return false;
        } else {
            TheadPoolProperties other = (TheadPoolProperties)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label71: {
                    Object this$timeOut = this.getTimeOut();
                    Object other$timeOut = other.getTimeOut();
                    if (this$timeOut == null) {
                        if (other$timeOut == null) {
                            break label71;
                        }
                    } else if (this$timeOut.equals(other$timeOut)) {
                        break label71;
                    }

                    return false;
                }

                Object this$queueSize = this.getQueueSize();
                Object other$queueSize = other.getQueueSize();
                if (this$queueSize == null) {
                    if (other$queueSize != null) {
                        return false;
                    }
                } else if (!this$queueSize.equals(other$queueSize)) {
                    return false;
                }

                label57: {
                    Object this$coreThreadNum = this.getCoreThreadNum();
                    Object other$coreThreadNum = other.getCoreThreadNum();
                    if (this$coreThreadNum == null) {
                        if (other$coreThreadNum == null) {
                            break label57;
                        }
                    } else if (this$coreThreadNum.equals(other$coreThreadNum)) {
                        break label57;
                    }

                    return false;
                }

                Object this$maxPoolSize = this.getMaxPoolSize();
                Object other$maxPoolSize = other.getMaxPoolSize();
                if (this$maxPoolSize == null) {
                    if (other$maxPoolSize != null) {
                        return false;
                    }
                } else if (!this$maxPoolSize.equals(other$maxPoolSize)) {
                    return false;
                }

                Object this$groupSize = this.getGroupSize();
                Object other$groupSize = other.getGroupSize();
                if (this$groupSize == null) {
                    if (other$groupSize == null) {
                        return true;
                    }
                } else if (this$groupSize.equals(other$groupSize)) {
                    return true;
                }

                return false;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof TheadPoolProperties;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $timeOut = this.getTimeOut();
        int result = result * 59 + ($timeOut == null ? 43 : $timeOut.hashCode());
        Object $queueSize = this.getQueueSize();
        result = result * 59 + ($queueSize == null ? 43 : $queueSize.hashCode());
        Object $coreThreadNum = this.getCoreThreadNum();
        result = result * 59 + ($coreThreadNum == null ? 43 : $coreThreadNum.hashCode());
        Object $maxPoolSize = this.getMaxPoolSize();
        result = result * 59 + ($maxPoolSize == null ? 43 : $maxPoolSize.hashCode());
        Object $groupSize = this.getGroupSize();
        result = result * 59 + ($groupSize == null ? 43 : $groupSize.hashCode());
        return result;
    }

    public String toString() {
        return "TheadPoolProperties(timeOut=" + this.getTimeOut() + ", queueSize=" + this.getQueueSize() + ", coreThreadNum=" + this.getCoreThreadNum() + ", maxPoolSize=" + this.getMaxPoolSize() + ", groupSize=" + this.getGroupSize() + ")";
    }
}

6

Atas ialah kandungan terperinci Bagaimana untuk melaksanakan program berbilang benang menggunakan Java?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:yisu.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam