-
Notifications
You must be signed in to change notification settings - Fork 0
Java02. ДЗ 01, Васильев Роман #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| group 'com.github.nizshee' | ||
| version '1.0-SNAPSHOT' | ||
|
|
||
| apply plugin: 'java' | ||
|
|
||
| sourceCompatibility = 1.8 | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| testCompile group: 'junit', name: 'junit', version: '4.11' | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #Sat Sep 10 19:34:51 MSK 2016 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rootProject.name = 'lazy-factory' | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.github.nizshee; | ||
|
|
||
| @SuppressWarnings("all") | ||
| public interface Lazy<T> { | ||
| T get(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.github.nizshee; | ||
|
|
||
|
|
||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Supplier; | ||
|
|
||
| @SuppressWarnings("all") | ||
| public class LazyFactory { | ||
|
|
||
| public static <T> Lazy<T> createLazySimple(Supplier<T> supplier) { | ||
| return new Lazy<T>() { | ||
| private Optional<T> optional = Optional.empty(); | ||
|
|
||
| @Override | ||
| public T get() { | ||
| if (!optional.isPresent()) { | ||
| optional = Optional.of(supplier.get()); | ||
| } | ||
| return optional.get(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public static <T> Lazy<T> createLazyConcurent(Supplier<T> supplier) { | ||
| return new Lazy<T>() { | ||
| private final Object monitor = new Object(); | ||
| private Optional<T> optional = Optional.empty(); | ||
|
|
||
| @Override | ||
| public T get() { | ||
| if (!optional.isPresent()) { | ||
| synchronized (monitor) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему this не подходит в качестве монитора? |
||
| if (!optional.isPresent()) { | ||
| optional = Optional.of(supplier.get()); | ||
| } | ||
| } | ||
| } | ||
| return optional.get(); | ||
| } | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут 4 ссылки) |
||
| } | ||
|
|
||
| public static <T> Lazy<T> createLazyLockFree(Supplier<T> supplier) { | ||
| return new Lazy<T>() { | ||
| final private AtomicReference<Optional<T>> optionalReference = new AtomicReference<>(Optional.empty()); | ||
|
|
||
| @Override | ||
| public T get() { | ||
| if (!optionalReference.get().isPresent()) { | ||
| Optional<T> newOptional = Optional.of(supplier.get()); | ||
| optionalReference.compareAndSet(Optional.empty(), newOptional); | ||
| } | ||
| return optionalReference.get().get(); | ||
| } | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. три ссылки - ref, optional и значение внутри optional |
||
| } | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
три ссылки: supplier, optional и значение внутри optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А почему значение внутри optional считается? Оно же не в Lazy объекте?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Иначе можно было написать так:
и все хранить в ds