Skip to content

Commit

Permalink
Prepare to Matrix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaOPs committed Oct 16, 2017
1 parent b89204e commit 56825e0
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 77 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
out
target
*.iml
log
276 changes: 199 additions & 77 deletions README.md

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# MasterJava Release Notes

### Masterjava 2
- Добавил в проект
- [Миграция базы через liquiBase](http://www.liquibase.org/quickstart.html)
- Реализация модели/DAO/JUnit
- Миграция DB
- Maven плагины copy-rename-maven-plugin, maven-antrun-plugin, liquibase-maven-plugin
- Авторизация в контейнере Tomcat
- Concurrent and distributed applications toolkit AKKA (отсылка почты через AKKA Actors)
- Асинхронные сервлеты 3.0
- [Выбор языка программирования](https://drive.google.com/file/d/0B9Ye2auQ_NsFZUVNakNxeUtGeFE)

- Pефакторинг
- Реализация модуля export: Thymeleaf и Upload
- Закэшировал `TemplateEngine`
- Загрузка файлов через API Servlet 3.0
- Сохранение в базу в batch-моде с обработкой конфликтов
- При обновлении пользователей теперь используем `INSERT ON CONFLICT`
- Работа со StAX (новый метод `startElement`)
44 changes: 44 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.javaops</groupId>
<artifactId>masterjava</artifactId>
<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>Master Java</name>
<url>https://github.com/JavaOPs/masterjava</url>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<finalName>masterjava</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
</dependencies>

<profiles>
</profiles>

<dependencyManagement>
</dependencyManagement>
</project>
14 changes: 14 additions & 0 deletions src/main/java/ru/javaops/masterjava/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.javaops.masterjava;

/**
* User: gkislin
* Date: 05.08.2015
*
* @link http://caloriesmng.herokuapp.com/
* @link https://github.com/JavaOPs/topjava
*/
public class Main {
public static void main(String[] args) {
System.out.format("Hello MasterJava!");
}
}
52 changes: 52 additions & 0 deletions src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.javaops.masterjava.matrix;

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

/**
* gkislin
* 03.07.2016
*/
public class MainMatrix {
private static final int MATRIX_SIZE = 1000;
private static final int THREAD_NUMBER = 10;

private final static ExecutorService executor = Executors.newFixedThreadPool(MainMatrix.THREAD_NUMBER);

public static void main(String[] args) throws ExecutionException, InterruptedException {
final int[][] matrixA = MatrixUtil.create(MATRIX_SIZE);
final int[][] matrixB = MatrixUtil.create(MATRIX_SIZE);

double singleThreadSum = 0.;
double concurrentThreadSum = 0.;
int count = 1;
while (count < 6) {
System.out.println("Pass " + count);
long start = System.currentTimeMillis();
final int[][] matrixC = MatrixUtil.singleThreadMultiply(matrixA, matrixB);
double duration = (System.currentTimeMillis() - start) / 1000.;
out("Single thread time, sec: %.3f", duration);
singleThreadSum += duration;

start = System.currentTimeMillis();
final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiply(matrixA, matrixB, executor);
duration = (System.currentTimeMillis() - start) / 1000.;
out("Concurrent thread time, sec: %.3f", duration);
concurrentThreadSum += duration;

if (!MatrixUtil.compare(matrixC, concurrentMatrixC)) {
System.err.println("Comparison failed");
break;
}
count++;
}
executor.shutdown();
out("\nAverage single thread time, sec: %.3f", singleThreadSum / 5.);
out("Average concurrent thread time, sec: %.3f", concurrentThreadSum / 5.);
}

private static void out(String format, double ms) {
System.out.println(String.format(format, ms));
}
}
61 changes: 61 additions & 0 deletions src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ru.javaops.masterjava.matrix;

import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

/**
* gkislin
* 03.07.2016
*/
public class MatrixUtil {

// TODO implement parallel multiplication matrixA*matrixB
public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException {
final int matrixSize = matrixA.length;
final int[][] matrixC = new int[matrixSize][matrixSize];

return matrixC;
}

// TODO optimize by https://habrahabr.ru/post/114797/
public static int[][] singleThreadMultiply(int[][] matrixA, int[][] matrixB) {
final int matrixSize = matrixA.length;
final int[][] matrixC = new int[matrixSize][matrixSize];

for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < matrixSize; j++) {
int sum = 0;
for (int k = 0; k < matrixSize; k++) {
sum += matrixA[i][k] * matrixB[k][j];
}
matrixC[i][j] = sum;
}
}
return matrixC;
}

public static int[][] create(int size) {
int[][] matrix = new int[size][size];
Random rn = new Random();

for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = rn.nextInt(10);
}
}
return matrix;
}

public static boolean compare(int[][] matrixA, int[][] matrixB) {
final int matrixSize = matrixA.length;
for (int i = 0; i < matrixSize; i++) {
for (int j = 0; j < matrixSize; j++) {
if (matrixA[i][j] != matrixB[i][j]) {
return false;
}
}
}
return true;
}
}
75 changes: 75 additions & 0 deletions src/main/java/ru/javaops/masterjava/service/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ru.javaops.masterjava.service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

public class MailService {
private static final String OK = "OK";

private static final String INTERRUPTED_BY_FAULTS_NUMBER = "+++ Interrupted by faults number";
private static final String INTERRUPTED_BY_TIMEOUT = "+++ Interrupted by timeout";
private static final String INTERRUPTED_EXCEPTION = "+++ InterruptedException";

public GroupResult sendToList(final String template, final Set<String> emails) throws Exception {
return new GroupResult(0, Collections.emptyList(), null);
}


// dummy realization
public MailResult sendToUser(String template, String email) throws Exception {
try {
Thread.sleep(500); //delay
} catch (InterruptedException e) {
// log cancel;
return null;
}
return Math.random() < 0.7 ? MailResult.ok(email) : MailResult.error(email, "Error");
}

public static class MailResult {
private final String email;
private final String result;

private static MailResult ok(String email) {
return new MailResult(email, OK);
}

private static MailResult error(String email, String error) {
return new MailResult(email, error);
}

public boolean isOk() {
return OK.equals(result);
}

private MailResult(String email, String cause) {
this.email = email;
this.result = cause;
}

@Override
public String toString() {
return '(' + email + ',' + result + ')';
}
}

public static class GroupResult {
private final int success; // number of successfully sent email
private final List<MailResult> failed; // failed emails with causes
private final String failedCause; // global fail cause

public GroupResult(int success, List<MailResult> failed, String failedCause) {
this.success = success;
this.failed = failed;
this.failedCause = failedCause;
}

@Override
public String toString() {
return "Success: " + success + '\n' +
"Failed: " + failed.toString() + '\n' +
(failedCause == null ? "" : "Failed cause" + failedCause);
}
}
}

0 comments on commit 56825e0

Please sign in to comment.