主頁 > 軟體設計 > 并發編程之Phaser類多階段任務(以遺傳演算法TSP問題為例)

并發編程之Phaser類多階段任務(以遺傳演算法TSP問題為例)

2021-02-07 14:32:17 軟體設計

目錄

一、認識Phaser類

1、Phaser類特征

2、任務注冊與注銷

3、同步階段變更

二、遺傳演算法

1、DataLoader類:加載資料

2、Individual類:問題所有可能解

3、GeneticOperators類:GA演算法核心

三、GA應用:Phaser類解決旅行商問題(TSP)

2、GeneticPhaser類:Java并發Phaser類實作

3、ConcurrentGeneticTask類:GA演算法階段任務執行

4、ConcurrentGeneticAlgorithm類:GA演算法并發實作

5、主類main

四、實驗結果

五、分析總結


一、認識Phaser類

學習Java并發有一段時間了,從基礎開始,認識了許多同步機制API,這次學習了Phaser類這個同步機制,重繪了以前的認識,再添一項新知識,

下面先認識一下Phaser是什么,然后結合實際應用,通過遺傳演算法經典應用的旅行商問題(TSP)的案例,更好地學習一下Phaser類同步機制的使用方法,

1、Phaser類特征

Java7的并發API引入了Phaser類,提供了強大的同步機制——分段器,它將任務劃分為多個階段執行,處理程序定義了明確的步驟,完成每個階段任務需要遵循先后順序,

主要特點:

  1. 分段器必須知道要控制的任務數量,Java中成為參與者(任務)的注冊機制,
  2. 任務完成一個階段后告知分段器,完成此階段之前,分段器使該任務處于休眠狀態,
  3. 分段器保存了一個整數值,存盤已經進行的階段變更數目,
  4. 當分段器做出階段變更時候,可以執行定制的代碼,
  5. 控制phaser的終止,
  6. 可以通過方法獲取參與者的數目和狀態,

2、任務注冊與注銷

這里根據上面說到的第1個特點,介紹注冊參與者的方法,注冊可以在執行任務開始時,也可以隨時注冊,

Phaser物件創建,常用的2個建構式:

  1. Phaser():創建一個有0個參與者的分段器,
  2. Phaser(int parties):創建一個給定數目參與者的分段器,

顯式創建方法:

  1. bulkRegisterl(int parties) :注冊給定數目的參與者,
  2. register():注冊一個參與者,

分段器控制的任務完成之后必須注銷,否則分段器會處于一直等待狀態,

注銷方法:

arriveAndDeRegister():告知分段器該任務已完成當前階段,而且不參與下一階段,

3、同步階段變更

分段器的主要目的是使那些可以分割為多階段演算法,以并發方式執行,任務只有完成當前階段,才能進入下一個階段,

Phaser類中有arrive,arriveAndDeRegister,arriveAndwaitAdvance三個方法來告知當前階段已經完成,但是當一個任務完成階段后未呼叫其中之一的方法,分段器可能對其余任務或階段進行阻塞,

因此,進入下一階段呼叫以下方法

  1. arriveAndwaitAdvance():告知分段器當前階段已經完成,進入下一階段,phaser阻塞該任務,直到所有參與的任務呼叫了其中一種同步方法,
  2. awaitAdvance(int phaser):告知分段器,若引數等于實際階段數,則等待當前階段結束;不等則回傳,

二、遺傳演算法

上面其實都是一些理論知識,對Phaser有一定的了解之后,開始在實際案例中學習,更進一步理解,遺傳演算法就是一個很好的例子,

遺傳演算法耳熟能詳,是基于自然選擇原理的一種自適應啟發式搜索演算法,用于最優化問題和在搜索問題中尋找更好地方案,換言之,“優勝劣汰,適者生存”,這也是從生物學引申而來的有類似原理的演算法,所以也使用了生物學的一些術語,

一些在遺傳演算法中使用的特定術語:

  1. 適應度函式:是主要目標,能夠使函式最大化或者最小化的解決方案,來確定方案的優劣,
  2. 種群:問題的所有可能方案的集合,初始集合可以隨機生成或使用啟發函式獲取初始解決方案,

初始種群之后,啟動包含3個階段的迭代程序:

  1. 選擇:在種群中選擇更好地個體,個體在適應度函式中有著較好的值,
  2. 交叉:上一步選擇優質個體后進行交叉,生成新一代個體,具體操作:parent1 X parent2 ——>> child1 + child2 ,主要依賴于待解決問題,
  3. 突變:可以應用突變運算子更改某個體的值,只對極少個體執行此操作,

結束標準:

  • 固定代數
  • 適應度函式設定的閾值
  • 找到滿足預定標準的方案
  • 時間限制
  • 手動停止

將遺傳演算法應用于TSP問題,目標找到一條最優路線,經過每個城市有且僅有一次,同時旅行總距離最短,

資料有兩份,包括15個城市和57個城市,二維矩陣中每個值表示城市之間的距離,

1、DataLoader類:加載資料

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;

public class DataLoader {

    /**
     * @param path
     * @return int[][]
     * @author Charzous
     * @date 2021/2/5 10:46
     *
     * 加載檔案,轉化為二維陣列
     */
    public static int[][] load(Path path) throws IOException {
        InputStream in = Files.newInputStream(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        String[] row = null;
        int[][] a;
        ArrayList<String> arrayList = new ArrayList<>();
        int rowNum = 0, colNum = 0, i = 0, j = 0;
        while ((line = reader.readLine()) != null) {
            arrayList.add(line);
            colNum = line.split(",").length;
        }
        rowNum = arrayList.size();
        a = new int[rowNum][colNum];
        int count = 0;

        for (String str : arrayList) {
            row = str.split(",");
            for (i = 0; i < colNum; i++) {
                a[count][i] = Integer.parseInt(row[i]);
            }
            count++;
        }
        return a;
    }
/**
 * @param args
 * @return void
 * @author Charzous
 * @date 2021/2/5 10:45
 *
 * 類內測驗,加載檔案
 */
//    public static void main(String[] args) throws IOException {
//        Path file = Paths.get("data/kn57_dist.txt");
//        int[][] a;
//        a = DataLoader.load(file);
//        for (int i = 0; i < a.length; i++) {
//            for (int j = 0; j < a[0].length; j++) {
//                System.out.print(a[i][j] + " ");
//            }
//            System.out.println();
//        }
//    }
}

2、Individual類:問題所有可能解

/**
 * 存放訪問不同城市的順序,TSP問題的所有可能解
 *
 * @author Charzous
 * @date 2021/2/5 10:53
 */
public class Individual implements Comparable<Individual> {

    private Integer[] chromosomes;
    private int value;

    public Integer[] getChromosomes() {
        return chromosomes;
    }

    public void setChromosomes(Integer[] chromosomes) {
        this.chromosomes = chromosomes;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public Individual(int size) {
        chromosomes = new Integer[size];
    }

    public Individual(Individual other) {
        chromosomes = other.getChromosomes().clone();
    }

    @Override
    public int compareTo(Individual o) {
        return Integer.compare(this.getValue(), o.getValue());
    }

    @Override
    public String toString() {
        String ret = "";
        for (Integer number : chromosomes)
            ret += number + ",";
        return ret;
    }
}

3、GeneticOperators類:GA演算法核心

遺傳演算法核心內部邏輯實作:選擇、交叉、個體和種群評估,詳見代碼注釋


import java.util.*;
/**
 * 實作遺傳演算法內部邏輯
 *
 * @author Charzous
 * @date 2021/2/5 11:21
 *
 */
public class GeneticOperators {
    /**
     * @param selected
     * @param numberOfIndividual
     * @param size
     * @return GeneticAlgorithm.Individual[]
     * @author Charzous
     * @date 2021/2/5 11:29
     *
     * 接收被選中的個體,交叉操作生成下一代種群
     */
    public static Individual[] crossover(Individual[] selected,int numberOfIndividual,int size){

        Individual population[] =new Individual[numberOfIndividual];
        Random random=new Random(System.nanoTime());
        for (int i=0;i<numberOfIndividual/2;i++){
            population[2*i]=new Individual(size);
            population[2*i+1]=new Individual(size);

            int p1Idx=random.nextInt(selected.length);
            int p2Idx;
            do {
                p2Idx=random.nextInt(selected.length);
            }while (p1Idx==p2Idx);

            Individual parent1=selected[p1Idx];
            Individual parent2=selected[p2Idx];

            crossover(parent1,parent2,population[2*i],population[2*i+1]);
        }
        return population;
    }

    /**
     * @param parent1
     * @param parent2
     * @param individual1
     * @param individual2
     * @return void
     * @author Charzous
     * @date 2021/2/5 11:31
     *
     *  parent1 X parent2 --> individual1 , individual2
     */
    public static void crossover(final Individual parent1,final Individual parent2,final Individual individual1,final Individual individual2){
        List<Integer> p1= Arrays.asList(parent1.getChromosomes());
        List<Integer> p2= Arrays.asList(parent2.getChromosomes());
        List<Integer> ch1=new ArrayList<Integer>(p1.size());
        List<Integer> ch2=new ArrayList<Integer>(p2.size());//p1 ? p2
        int size=p1.size();
        Random random=new Random();
        int number1=random.nextInt(size-1);
        int number2;
        //1,14,6,2,3,5,7,0,12,4,11,13,9,8,10,
        do {
            number2=random.nextInt(size);
        }while (number1==number2);

        int start=Math.min(number1,number2);
        int end=Math.max(number1,number2);
        ch1.addAll(p1.subList(start,end));
        ch2.addAll(p2.subList(start,end));

        int currentCity=0;
        int currentCityParent1=0;
        int currentCityParent2=0;
        for (int i = 0; i < size; i++) {
            currentCity=(end+i)%size;
            currentCityParent1=p1.get(currentCity);
            currentCityParent2=p2.get(currentCity);

            if (!ch1.contains(currentCityParent2))
                ch1.add(currentCityParent2);
            if (!ch2.contains(currentCityParent1))
                ch2.add(currentCityParent1);
        }

        Collections.rotate(ch1,start);
        Collections.rotate(ch2,start);
        individual1.setChromosomes(ch1.toArray(individual1.getChromosomes()));
        individual2.setChromosomes(ch2.toArray(individual2.getChromosomes()));
    }
    /**
     * @param population
     * @return GeneticAlgorithm.Individual[]
     * @author Charzous
     * @date 2021/2/5 11:23
     *
     * 獲取種群的最優個體,回傳種群一半的個體,可使用最適合函式選擇
     */
    public static Individual[] selection(Individual[] population){
        Individual selected[] =new Individual[population.length/2];

        for (int i = 0; i < selected.length; i++) {
            selected[i]=new Individual(population[i]);
        }
        return selected;
    }

    /**
     * @param numberOfIndividual
     * @param size
     * @return GeneticAlgorithm.Individual[]
     * @author Charzous
     * @date 2021/2/5 11:24
     *
     * 創建一個群,城市數目size
     */
    public static Individual[] initialize(int numberOfIndividual,int size){
        Individual population[] =new Individual[numberOfIndividual];
        for (int i = 0; i < numberOfIndividual; i++) {
            population[i]=new Individual(size);
            initialize(population[i].getChromosomes());
        }
        return population;
    }

    /**
     * @param chromosomes
     * @return void
     * @author Charzous
     * @date 2021/2/5 11:25
     *
     * 隨機初始化某一個體的染色體,生成合法的個體(即每個城市只訪問一次)
     */
    public static void initialize(Integer[] chromosomes){
        int size=chromosomes.length;
        List<Integer> values=new ArrayList<Integer>(size);

        for (int i = 0; i < size; i++) {
            values.add(i);
        }
        Collections.shuffle(values,new Random(System.nanoTime()));

        for (int i = 0; i < size; i++) {
            chromosomes[i]=values.get(i);
        }
    }
    /**
     * @param population
     * @param distanceMatrix
     * @return void
     * @author Charzous
     * @date 2021/2/5 11:33
     *
     * 接收距離矩陣,將適應函式應用到全部個體,對種群排序
     */
    public static void evaluate(Individual[] population,int[][] distanceMatrix){
        for (Individual individual:population) {
            evaluate(individual,distanceMatrix);
        }
        Arrays.sort(population);
    }
    /**
     * @param individual
     * @param distanceMatrix
     * @return void
     * @author Charzous
     * @date 2021/2/5 11:36
     *
     * 接將適應函式應用到一個個體,結果加入到個體集合,以供對種群所有進行排序
     */
    public static void evaluate(Individual individual,int[][] distanceMatrix){
        Integer chromosomes[]=individual.getChromosomes();
        int totalDistance=0;
        int source,destination;
        for (int i = 0; i < chromosomes.length-1; i++) {
            source=chromosomes[i];
            destination=chromosomes[i+1];
            totalDistance+=distanceMatrix[source][destination];
        }
        source=chromosomes[chromosomes.length-1];
        destination=chromosomes[0];
        totalDistance+=distanceMatrix[source][destination];

        individual.setValue(totalDistance);
    }
}

三、GA應用:Phaser類解決旅行商問題(TSP)

1、SharedData類:共享物件

import java.util.concurrent.atomic.AtomicInteger;
/**
 * 存放任務之間共享的物件
 *
 * @author Charzous
 * @date 2021/2/5 15:16
 *
 */
public class SharedData {
    private Individual[] population;
    private Individual[] selected;
    private AtomicInteger index;
    private Individual best;
    private int[][] distanceMatrix;

    public SharedData() {
        index=new AtomicInteger();
    }

    public Individual[] getPopulation() {
        return population;
    }

    public void setPopulation(Individual[] population) {
        this.population = population;
    }

    public Individual[] getSelected() {
        return selected;
    }

    public void setSelected(Individual[] selected) {
        this.selected = selected;
    }

    public AtomicInteger getIndex() {
        return index;
    }

    public void setIndex(AtomicInteger index) {
        this.index = index;
    }

    public Individual getBest() {
        return best;
    }

    public void setBest(Individual best) {
        this.best = best;
    }

    public int[][] getDistanceMatrix() {
        return distanceMatrix;
    }

    public void setDistanceMatrix(int[][] distanceMatrix) {
        this.distanceMatrix = distanceMatrix;
    }
}

2、GeneticPhaser類:Java并發Phaser類實作

import java.util.Arrays;
import java.util.concurrent.Phaser;

public class GeneticPhaser extends Phaser {
    private SharedData data;

    public GeneticPhaser(int parties, SharedData data) {
        super(parties);
        this.data=data;
    }
    /**
     * @param phase
     * @param registeredParties
     * @return boolean
     * @author Charzous
     * @date 2021/2/5 15:20
     *
     * 多載onAdvance方法,使所有任務完成第一階段之后執行代碼
     */
    @Override
    protected boolean onAdvance(int phase, int registeredParties) {
        int realPhase=phase%3;
        if (registeredParties>0) {
            switch (realPhase) {
                case 0:
                case 1:
                    data.getIndex().set(0);
                    break;
                case 2:
                    Arrays.sort(data.getPopulation());
                    if  (data.getPopulation()[0].getValue() < data.getBest().getValue()) {
                        data.setBest(data.getPopulation()[0]);
                    }
                    break;
            }
            return false;
        }
        return true;
    }

    public SharedData getData() {
        return data;
    }

    public void setData(SharedData data) {
        this.data = data;
    }
}

3、ConcurrentGeneticTask類:GA演算法階段任務執行

import JavaConcurrencyProgramming.chapter06.GeneticAlgorithm.GeneticOperators;
import JavaConcurrencyProgramming.chapter06.GeneticAlgorithm.Individual;

import java.util.Random;
/**
 * 執行遺傳演算法各個階段的任務
 *
 * @author Charzous
 * @date 2021/2/5 15:28
 *
 */
public class ConcurrentGeneticTask implements Runnable {
    private GeneticPhaser phaser;
    private SharedData data;
    private int numberOfGenerations;
    private boolean main;

    public ConcurrentGeneticTask(GeneticPhaser phaser, int numberOfGenerations,
                                 boolean main) {
        this.phaser = phaser;
        this.numberOfGenerations = numberOfGenerations;
        this.main = main;
        this.data = phaser.getData();
    }

    @Override
    public void run() {
        Random random = new Random((System.nanoTime()));
        for (int i = 0; i < numberOfGenerations; i++) {
            if (main)
                data.setSelected(GeneticOperators.selection(data.getPopulation()));

            phaser.arriveAndAwaitAdvance();

            //交叉操作
            int individualIndex;
            do {
                individualIndex = data.getIndex().getAndAdd(2);
                if (individualIndex < data.getPopulation().length) {
                    int secondIndividual = individualIndex++;

                    int p1Index = random.nextInt(data.getSelected().length);
                    int p2Index;
                    do {
                        p2Index = random.nextInt(data.getSelected().length);
                    } while (p1Index == p2Index);

                    Individual parent1 = data.getSelected()[p1Index];
                    Individual parent2 = data.getSelected()[p2Index];
                    Individual individual1 = data.getPopulation()[individualIndex];
                    Individual individual2 = data.getPopulation()[secondIndividual];

                    GeneticOperators.crossover(parent1, parent2, individual1, individual2);
                }
            } while (individualIndex < data.getPopulation().length);
            phaser.arriveAndAwaitAdvance();

            do {
                individualIndex = data.getIndex().getAndIncrement();
                if (individualIndex < data.getPopulation().length) {
                    GeneticOperators.evaluate(data.getPopulation()[individualIndex], data.getDistanceMatrix());
                }
            } while (individualIndex < data.getPopulation().length);
            phaser.arriveAndAwaitAdvance();
        }
        phaser.arriveAndDeregister();
    }
}

4、ConcurrentGeneticAlgorithm類:GA演算法并發實作

/**
 * 遺傳演算法并發演算法
 * @author Charzous
 * @date 2021/2/5 15:30
 *
 */
public class ConcurrentGeneticAlgorithm {
    private int numberOfGenerations;
    private int numberOfIndividuals;
    private int[][] distanceMatrix;
    private int size;

    public ConcurrentGeneticAlgorithm(int[][] distanceMatrix, int numberOfGenerations, int numberOfIndividuals) {
        this.distanceMatrix = distanceMatrix;
        this.numberOfGenerations = numberOfGenerations;
        this.numberOfIndividuals = numberOfIndividuals;
        size = distanceMatrix.length;
    }

    public Individual calculate() {

        Individual[] population = GeneticOperators.initialize(numberOfIndividuals, size);
        GeneticOperators.evaluate(population, distanceMatrix);

        SharedData data = new SharedData();
        data.setPopulation(population);
        data.setDistanceMatrix(distanceMatrix);
        data.setBest(population[0]);

        int numTasks = Runtime.getRuntime().availableProcessors();
        GeneticPhaser phaser = new GeneticPhaser(numTasks, data);

        ConcurrentGeneticTask[] tasks = new ConcurrentGeneticTask[numTasks];
        Thread[] threads = new Thread[numTasks];

        tasks[0] = new ConcurrentGeneticTask(phaser, numberOfGenerations, true);
        for (int i = 1; i < numTasks; i++) {
            tasks[i] = new ConcurrentGeneticTask(phaser, numberOfGenerations, false);
        }

        for (int i = 0; i < numTasks; i++) {
            threads[i] = new Thread(tasks[i]);
            threads[i].start();
        }

        for (int i = 0; i < numTasks; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return data.getBest();
    }
}

5、主類main

import java.io.IOException;
import java.nio.file.Paths;

public class ConcurrentMain {
    public static void main(String[] args) throws IOException {

        long start, end;

        int generations = 1000;
        int individuals = 1000;

        String[] names={"kn57_dist"};//"lau15_dist" ,
        for (String name:names) {
            int[][] distanceMatrix = DataLoader.load(Paths.get("data", name + ".txt"));

            ConcurrentGeneticAlgorithm concurrentGeneticAlgorithm = new ConcurrentGeneticAlgorithm(distanceMatrix, generations,
                    individuals);
            start = System.currentTimeMillis();
            Individual result = concurrentGeneticAlgorithm.calculate();
            end = System.currentTimeMillis();
            System.out.println("=======================================");
            System.out.println("Example:"+name);
            System.out.println("Generations: " + generations);
            System.out.println("Population: " + individuals);
            System.out.println("Execution Time: " + (end - start)+"ms");
            System.out.println("Best Individual: " + result);
            System.out.println("Total Distance: " + result.getValue());
            System.out.println("=======================================");
        }

    }
}

四、實驗結果

資料和實驗引數設定:

generations = 1000;
individuals = 1000;

資料:lau15_dist.txt(15個城市)

測驗執行10次,平均執行時間為500ms左右,

當然,種群數和代數可以設定其他值,資料集使用可以更換,進行實驗,

為了比較并發版本執行表現的提升,簡單實作了單執行緒版本,只需要將ConcurrentGeneticAlgorithm類里面的并發部分修改為單執行緒方法,結果如下:

測驗執行10次,平均執行時間為690ms左右,對比之下可看出并發的性能提升,

五、總結

這篇記錄Phaser類同步機制的原理和實踐理解,學習Java并發有一段時間了,從基礎開始,認識了許多同步機制API,這次學習了Phaser類這個同步機制,重繪了以前的認識,再添一項新知識,先認識一下Phaser是什么,然后結合實際應用,通過遺傳演算法經典應用的旅行商問題(TSP)的案例,更好地學習一下Phaser類同步機制的使用方法,對Phaser一些理論知識有一定的了解之后,開始在實際案例中學習,更進一步理解,遺傳演算法就是一個很好的例子,掌握了分段器在并發編程中的基礎使用,也希望在之后的學習應用能夠有所幫助,

如果覺得不錯歡迎“一鍵三連”哦,點贊收藏關注,有問題直接評論,交流學習!

Java并發編程系列文章:

  1. 原創 Java并發API案例分析之并發設計原理[https://blog.csdn.net/Charzous/article/details/112603639]
  2. 原創 Java并發(Runnable+Thread)實作硬碟檔案搜索[https://blog.csdn.net/Charzous/article/details/112853937]
  3. 原創 并發編程之Callable/Future介面(以單詞最佳匹配演算法為例)[https://blog.csdn.net/Charzous/article/details/113338669]
  4. 并發編程之Phaser類多階段任務(以遺傳演算法TSP問題為例)[https://blog.csdn.net/Charzous/article/details/113698041]

我的CSDN博客:https://blog.csdn.net/Charzous/article/details/113698041

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/257498.html

標籤:其他

上一篇:金三銀四如何短期拔高?這份排坑的Android面筋必須收藏!「附高質學習資料」

下一篇:用Python撰寫成績管理分析系統(故事升級版)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more