Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: java
jdk:
- oraclejdk8
os:
- linux
script:
- chmod +x buildscript.sh && ./buildscript.sh
36 changes: 36 additions & 0 deletions 01.Lazy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>01</groupId>
<artifactId>Lazy</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>15.0</version>
</dependency>
</dependencies>


</project>
8 changes: 8 additions & 0 deletions 01.Lazy/src/main/java/ru/spbau/mit/alyokhina/Lazy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.spbau.mit.alyokhina;
/**
* Lazy computing interface
* @param <T> type of returned object
*/
public interface Lazy<T> {
T get();
}
102 changes: 102 additions & 0 deletions 01.Lazy/src/main/java/ru/spbau/mit/alyokhina/LazyFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ru.spbau.mit.alyokhina;

import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

/**
* Class for create object of Lazy
* There is a single-threaded and multithreaded version of the creation
*
* @param <T> type of Lazy object
*/
public class LazyFactory<T> {
/**
* Create Lazy object for single thread
*
* @param supplier the Lazy object is created on the basis of calculations (represented by the supplier)
* @param <T> type of Lazy object
* @return Lazy object
*/
@NotNull
public static <T> Lazy<T> createLazySingleThreadMode(Supplier<T> supplier) {
return new LazySingleThreadMode<>(supplier);
}

/**
* Create Lazy object for many threads
*
* @param supplier the Lazy object is created on the basis of calculations (represented by the supplier)
* @param <T> type of Lazy object
* @return Lazy object
*/
@NotNull
public static <T> Lazy<T> createLazyMultiThreadedMode(Supplier<T> supplier) {
return new LazyMultiThreadMode<>(supplier);
}


/**
* class implementing interface Lazy for one thread
*
* @param <T> type of Lazy object
*/
private static class LazySingleThreadMode<T> implements Lazy<T> {
/** The Lazy object is created on the basis of calculations (represented by the supplier) */
private Supplier<T> supplier;

/** Value that came after calling the supplier (null - if there was no call) */
private T ans;

/** Constructor */
private LazySingleThreadMode(Supplier<T> supplier) {
this.supplier = supplier;
}

/**
* If the value has not yet been calculated, then calculate
*
* @return the value that was received
*/
@Override
public T get() {
if (ans == null) {
ans = supplier.get();
}
return ans;
}
}

/**
* class implementing interface Lazy for many threads
*
* @param <T> type of Lazy object
*/
private static class LazyMultiThreadMode<T> implements Lazy<T> {
/** The Lazy object is created on the basis of calculations (represented by the supplier) */
private volatile Supplier<T> supplier;

/** Value that came after calling the supplier (null - if there was no call) */
private T ans;

/** Constructor */
private LazyMultiThreadMode(Supplier<T> supplier) {
this.supplier = supplier;
}

/**
* If the value has not yet been calculated, then calculate
*
* @return the value that was received
*/
@Override
public T get() {
synchronized (this) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а можно как-то засинхронизировать только код, который будет инициализировать значение?

if (ans == null) {
ans = supplier.get();
}
}
return ans;
}
}
}
79 changes: 79 additions & 0 deletions 01.Lazy/src/test/java/ru/spbau/mit/alyokhina/LazyFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ru.spbau.mit.alyokhina;

import org.junit.Test;
import java.util.function.Supplier;
import static org.junit.Assert.*;

public class LazyFactoryTest {
@Test
public void testCreateLazySingleThreadMode() {
Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(() -> 5);
assertEquals((Integer) 5, test.get());
assertEquals((Integer) 5, test.get());
}

@Test
public void testCreateLazySingleThreadModeIfSupplierGetNull() {
Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(() -> null);
assertEquals(null, test.get());
assertEquals(null, test.get());
}

@Test
public void testCreateLazySingleThreadModeIfSupplierChangeValue() {
Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(new Supplier<Integer>() {
private boolean flag = false;

@Override
public Integer get() {
Integer ans = flag ? 5 : 6;
flag = true;
return ans;
}
});
assertEquals((Integer) 6, test.get());
assertEquals((Integer) 6, test.get());
}

@Test
public void testCreateLazyMultiThreadedModeForOneThread() {
Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(() -> 5);
assertEquals((Integer) 5, test.get());
assertEquals((Integer) 5, test.get());
}

@Test
public void testCreateLazyMultiThreadedModeForThreads() {
Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(() -> 5);
Thread[] threads = new Thread[1000];
for (int i = 0; i < 1000; i++) {
threads[i] = new Thread(() -> assertEquals((Integer) 5, test.get()));
}
for (Thread thread : threads) {
thread.run();
}
}

@Test
public void testCreateLazyMultiThreadedModeForThreadsIfSupplierChange() throws Exception {
Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(new Supplier<Integer>() {
private boolean flag = false;

@Override
public Integer get() {
Integer ans = flag ? 5 : 6;
flag = true;
return ans;
}
});
Thread[] threads = new Thread[1000];
for (int i = 0; i < 1000; i++) {

threads[i] = new Thread(() -> assertEquals((Integer) 6, test.get()));
}
for (Thread thread : threads) {
thread.run();
}
}

}
9 changes: 9 additions & 0 deletions buildscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
files=$(find . -maxdepth 1 -type d | grep "./0.*")
for file in $files
do
cd $file
mvn test -B
cd ../
done