java 多线程模板

import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

public class MultiThread {

    public static void main(String[] args) {

        Logger log = Logger.getAnonymousLogger();
        log.info("in main");
        log.info("123456 md5:"+md5("123456"));
        // 1. prepare data for multi thread job
        LinkedBlockingDeque<Object> rawMaterialQue = new LinkedBlockingDeque<>();
        for (int i = 0; i < 1000000; i++) {
            rawMaterialQue.add(UUID.randomUUID().toString());
        }
        log.info("ready rawMaterialQue:"+rawMaterialQue.size());
        // 2. run thread
        LinkedBlockingDeque<Object> productQue = new LinkedBlockingDeque<>();
        int threadLimit = 20;
        AtomicInteger globalCounter = new AtomicInteger(0);
        // add stop marker before run sub thread
        String stopMarker = "stopJob";
        for (int i = 0; i < threadLimit; i++) {
            rawMaterialQue.add(stopMarker);
        }
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < threadLimit; i++) {
            Thread threadOne = new Thread(()->{
                try {
                    // current thread job counter
                    int jobCounter = 0;
                    for (;;){
                        Object rawMaterialOne = rawMaterialQue.take();
                        // stop marker ?
                        if (Objects.equals(rawMaterialOne,stopMarker)){
                            log.info("sub thread ["+ Thread.currentThread().getName() +"] done:"+ jobCounter +" "+globalCounter.get());
                            return;
                        }
                        // do task
                        globalCounter.incrementAndGet();
                        jobCounter++;
                        productQue.put(md5((String) rawMaterialOne));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            threadOne.start();
            threadList.add(threadOne);
        }
        //3. wait thread done
        log.info("waiting sub thread");
        try {
            for(Thread one:threadList) {
                one.join();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("all sub thread done:" +globalCounter +" "+productQue.size());

    }

    public static String md5(String str){
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] digest = md.digest();
            String myHash = DatatypeConverter
                    .printHexBinary(digest);
            return myHash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

你可能感兴趣的:(java)