Multi-thread sharing test

com.xiaohui.intent.ActiveProcess

package com.xiaohui.intent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * todo
 *
 * @author huizi
 * @date 2023/1/2 15:38
 **/
public class ActiveProcess {
    private List<Map<String,MyProcess>> threds;

    ActiveProcess(int size){
        threds = new ArrayList<>(size);
    }

    public List<Map<String, MyProcess>> getThreds() {
        return threds;
    }

    public void setThreds(List<Map<String, MyProcess>> threds) {
        this.threds = threds;
    }

    public String getF1process(){
        long count = threds.stream().map(item -> item.get(item.keySet().toArray()[0])).filter(item -> item.isF1()).count();
        return count*100D/threds.size()+"";
    }
    public String getF2process(){
        long count = threds.stream().map(item -> item.get(item.keySet().toArray()[0])).filter(item -> item.isF2()).count();
        return count*100D/threds.size()+"";
    }
    public String getF3process(){
        long count = threds.stream().map(item -> item.get(item.keySet().toArray()[0])).filter(item -> item.isF3()).count();
        return count*100D/threds.size()+"";
    }
    public String getF4process(){
        long count = threds.stream().map(item -> item.get(item.keySet().toArray()[0])).filter(item -> item.isF4()).count();
        return count*100D/threds.size()+"";
    }

    @Override
    public String toString() {
        return "ActiveProcess{" +
                "threds=" + threds +
                '}';
    }
}

com.xiaohui.attempt.IntentMgr

package com.xiaohui.intent;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * todo
 *
 * @author huizi
 * @date 2023/1/2 15:14
 **/
public class IntentMgr {

    public void active(){
        List<Map<String,String>> models = new ArrayList();
        Map m1 = new HashMap();
        m1.put("name", "m1");
        models.add(m1);
        Map m2 = new HashMap();
        m2.put("name", "m2");
        Map m3 = new HashMap();
        m3.put("name", "m3");
        models.add(m2);
        models.add(m3);
        ActiveProcess activeProcess = new ActiveProcess(models.size());
        AtomicReference<ActiveProcess> arp = new AtomicReference<>();
        arp.set(activeProcess);
        models.stream().forEach(model -> {
            new Thread(()->{
                try {
//                    Map<String, MyProcess> map = new HashMap<>();
//                    map.put(model.get("name"),new MyProcess());
//                    arp.get().getThreds().add(map);
//                    execute(model,arp);
                    Map<String, MyProcess> map = new HashMap<>();
                    map.put(model.get("name"),new MyProcess());
                    activeProcess.getThreds().add(map);
                    execute2(model,activeProcess);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },model.get("name")).start();
        });
    }

    /**
     * 自启动 自配置 自激活 自生成
     * @param map
     */
    private void execute(Map<String,String> map,AtomicReference<ActiveProcess> arp) throws InterruptedException {
        //clmodel中楼层名称
        String name = map.get("name");
        System.out.println(Thread.currentThread().getName()+ " 开始执行....");
        ActiveProcess activeProcess = arp.get();
        MyProcess myProcess = activeProcess.getThreds().stream().filter(item ->
               item.keySet().contains(name)
        ).findFirst().get().get(name);
        Thread.sleep((long)(Math.random()*100-1));
        myProcess.setF1(true);
        System.out.println(name+" 自启动完成...."+activeProcess.getF1process());

        Thread.sleep(1000);
        myProcess.setF2(true);
        System.out.println(name+" 自配置完成...."+activeProcess.getF2process());

        Thread.sleep(1000);
        myProcess.setF3(true);
        System.out.println(name+" 自激活完成...."+activeProcess.getF3process());

        Thread.sleep(1000);
        myProcess.setF4(true);
        System.out.println(name+" 自生成完成...."+activeProcess.getF4process());
    }
    private void execute2(Map<String,String> map,ActiveProcess activeProcess) throws InterruptedException {
        //clmodel中楼层名称
        String name = map.get("name");
        System.out.println(Thread.currentThread().getName()+ " 开始执行....");
        MyProcess myProcess = activeProcess.getThreds().stream().filter(item ->
               item.keySet().contains(name)
        ).findFirst().get().get(name);
        Thread.sleep((long)(Math.random()*100-1));
        myProcess.setF1(true);
        System.out.println(name+" 自启动完成...."+activeProcess.getF1process());

        Thread.sleep(1000);
        myProcess.setF2(true);
        System.out.println(name+" 自配置完成...."+activeProcess.getF2process());

        Thread.sleep(1000);
        myProcess.setF3(true);
        System.out.println(name+" 自激活完成...."+activeProcess.getF3process());

        Thread.sleep(1000);
        myProcess.setF4(true);
        System.out.println(name+" 自生成完成...."+activeProcess.getF4process());
    }

}

com.xiaohui.intent.MyProcess

package com.xiaohui.intent;

/**
 * todo
 *
 * @author huizi
 * @date 2023/1/2 16:32
 **/
public class MyProcess {
    private boolean isF1;
    private boolean isF2;
    private boolean isF3;
    private boolean isF4;

    public MyProcess() {
    }

    public boolean isF1() {
        return isF1;
    }

    public void setF1(boolean f1) {
        isF1 = f1;
    }

    public boolean isF2() {
        return isF2;
    }

    public void setF2(boolean f2) {
        isF2 = f2;
    }

    public boolean isF3() {
        return isF3;
    }

    public void setF3(boolean f3) {
        isF3 = f3;
    }

    public boolean isF4() {
        return isF4;
    }

    public void setF4(boolean f4) {
        isF4 = f4;
    }

    @Override
    public String toString() {
        return "MyProcess{" +
                "isF1=" + isF1 +
                ", isF2=" + isF2 +
                ", isF3=" + isF3 +
                ", isF4=" + isF4 +
                '}';
    }
}

com/xiaohui/intent/MainTest.java

package com.xiaohui.intent;

/**
 * todo
 *
 * @author huizi
 * @date 2023/1/2 15:21
 **/
public class MainTest {
    public static void main(String[] args) {
        new IntentMgr().active();
    }
}

Guess you like

Origin blog.csdn.net/liuhenghui5201/article/details/128523117