<repo_name>megabites2013/threadtest<file_sep>/src/main/java/com/wcc/threadtest/generator/DetailGenerator.java
package com.wcc.threadtest.generator;

import com.wcc.threadtest.model.Detail;
import com.wcc.threadtest.model.Person;
import com.wcc.threadtest.model.Sex;
import com.wcc.threadtest.repository.DetailRepository;
import com.wcc.threadtest.threadtest.service.RandomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import lombok.Data;


@Component
public class DetailGenerator implements Generator {

    @Autowired
    private DetailRepository detailRepository;

    @Autowired
    private RandomService r;

    @Override
    public Person generate(Person person) {
        foo();
        Detail d = new Detail();
        d.setAge(r.getRandomInt());
        d.setLength(r.getRandomFloat());
        d.setWeight(r.getRandomFloat());
        d.setSalary(r.getRandomFloat());
        d.setSex(Sex.getRandom());

        detailRepository.save(d);
        person.setInfo(d);

        //System.out.print("D");

        return person;
    }
}
<file_sep>/src/main/java/com/wcc/threadtest/service/InitService.java
package com.wcc.threadtest.service;

import com.wcc.threadtest.controller.PersonController;
import com.wcc.threadtest.model.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

@Slf4j
@Service
public class InitService {

  @Autowired private ExecutorService executorService;

  @Autowired private com.wcc.threadtest.threadtest.service.RandomService r;

  @Autowired private PersonController controller;

  private int howManyGroups = 288;

  private Map<Integer, List<Person>> works;

  public InitService() {
    works = new HashMap();
  }

  public void populateDB() {

    long startTime = System.nanoTime();
    long count = 0;

    while (howManyGroups > 0) {

      getPeoplesParallel(howManyGroups);
      howManyGroups--;
    }
    long endTime = System.nanoTime();

    for (List<Person> list : works.values()) {
      count += list.size();
    }

    log.info(
        "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ done = " + count + "  in " + (endTime - startTime));
  }

  private void getPeoplesParallel(int howManyGroups) {

    executorService.submit(
        () -> {
          getPeoples(howManyGroups);
        });
  }

  public void getPeoples(int howManyGroups) {
    List<Person> ps = new ArrayList<>();

    Long numbers = r.getRandomLong();

    if (howManyGroups == 1 || howManyGroups == 200 || howManyGroups == 100) {
      numbers = 10000L;
    } else {
      numbers = 30L;
    }

    log.info("---------Group No:" + howManyGroups + " --------------> " + numbers + " Persons");

    while (numbers > 0) {

      ps.add(controller.createPerson("" + howManyGroups + "-" + numbers));
      numbers--;
    }

    log.info("++++++++++++ created " + ps.size() + " for Group No: " + howManyGroups + " +++ ");
    works.put(howManyGroups, ps);
  }

  public void getPeoplesFork(int howManyGroups) {
    List<Person> ps = new ArrayList<>();

    Long numbers = r.getRandomLong();

    if (howManyGroups == 1 || howManyGroups == 200 || howManyGroups == 100) {
      numbers = 10000L;
    } else {
      numbers = 30L;
    }

    log.info("---------Group No:" + howManyGroups + " --------------> " + numbers + " Persons");

    while (numbers > 0) {

      ps.add(controller.createPersonFork("" + howManyGroups + "-" + numbers));
      numbers--;
    }

    log.info("++++++++++++ created " + ps.size() + " for Group No: " + howManyGroups + " +++ ");
    works.put(howManyGroups, ps);
  }
}
<file_sep>/src/main/java/com/wcc/threadtest/generator/DocumentGenerator.java
package com.wcc.threadtest.generator;

import com.wcc.threadtest.model.Document;
import com.wcc.threadtest.model.Person;
import com.wcc.threadtest.repository.DocumentRepository;
import com.wcc.threadtest.threadtest.service.RandomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.Data;

@Component
public class DocumentGenerator implements Generator {

    @Autowired
    private DocumentRepository documentRepository;

    @Autowired
    private RandomService r;

    @Override
    public Person generate(Person person) {
        foo();
        Document document = new Document();
        document.setDocumentStr("setDocumentStr"+r.getRandomInt());
        document.setExpireAt(r.getRandomDate());
        document.setIssuedAt(r.getRandomDate());
        document.setType("DocuTypo"+r.getRandomInt());
        documentRepository.save(document);
        person.getDocument().add(document);
        //System.out.print("T");

        return person;

    }
}
<file_sep>/src/main/java/com/wcc/threadtest/model/Person.java
package com.wcc.threadtest.model;

import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Data
public class Person {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    String id;
    String fName;
    String surName;
    Date birthDay;
    @OneToMany
    List<Document> document = new ArrayList<>();

    @OneToMany
    List<Citizen> citizen = new ArrayList<>();

    @OneToOne
    Detail info;


}
<file_sep>/src/main/java/com/wcc/threadtest/model/Sex.java
package com.wcc.threadtest.model;

import javax.persistence.Entity;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public enum Sex {
    Male,Female,Unkown,Both,WontTell;

    private static final List<Sex> VALUES =
            Collections.unmodifiableList(Arrays.asList(values()));
    private static final int SIZE = VALUES.size();
    private static final Random RANDOM = new Random();

    public static Sex getRandom()  {
        return VALUES.get(RANDOM.nextInt(SIZE));
    }

}
<file_sep>/src/main/java/com/wcc/threadtest/repository/PersonRepository.java
package com.wcc.threadtest.repository;

import com.wcc.threadtest.model.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {}
<file_sep>/src/main/java/com/wcc/threadtest/ThreadtestApplication.java
package com.wcc.threadtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootApplication
public class ThreadtestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThreadtestApplication.class, args);
    }


    @Bean
    public ExecutorService getExecutorService() {
        return Executors.newFixedThreadPool(20);

    }
}

<file_sep>/src/main/java/com/wcc/threadtest/controller/PersonController.java
package com.wcc.threadtest.controller;

import com.wcc.threadtest.generator.Generator;
import com.wcc.threadtest.model.Person;
import com.wcc.threadtest.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

@Controller
public class PersonController {


    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private List<Generator> generators;

    @Autowired
    private ExecutorService executorService;

    @Autowired
    private com.wcc.threadtest.threadtest.service.RandomService r;


    public Person createPerson(String s) {


        List<Generator> b = new ArrayList<Generator>(generators);

        Person p = new Person();
        p.setFName(s);
        p.setSurName(s);
        p.setBirthDay(r.getRandomDate());


        for (Generator generator : b) {

            p=generator.generate(p);


        }


        personRepository.save(p);

        return p;
    }

    public Person createPersonFork(String s) {

        List<Generator> b = new ArrayList<Generator>(generators);

        Person p = new Person();
        p.setFName(s);
        p.setSurName(s);
        p.setBirthDay(r.getRandomDate());

        b.parallelStream().forEach(g -> g.generate(p));

        personRepository.save(p);

        return p;
    }






}
