Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SharplEr committed May 9, 2018
1 parent c56f6d1 commit 293d4b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 155 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/cactoos/func/SyncFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.concurrent.Callable;
import org.cactoos.Func;
import org.cactoos.Proc;

/**
* Func that is thread-safe.
Expand All @@ -49,6 +50,16 @@ public final class SyncFunc<X, Y> implements Func<X, Y> {
*/
private final Object lock;

/**
* Ctor.
* @param runnable Func original
* @param result Result to return
* @since 0.32
*/
public SyncFunc(final Runnable runnable, final Y result) {
this(new FuncOf<>(runnable, result));
}

/**
* Ctor.
* @param callable Func original
Expand All @@ -58,6 +69,16 @@ public SyncFunc(final Callable<Y> callable) {
this(new FuncOf<>(callable));
}

/**
* Ctor.
* @param proc Func original
* @param result Result to return
* @since 0.32
*/
public SyncFunc(final Proc<X> proc, final Y result) {
this(new FuncOf<>(proc, result));
}

/**
* Ctor.
* @param fnc Func original
Expand Down
73 changes: 0 additions & 73 deletions src/main/java/org/cactoos/func/SyncProc.java

This file was deleted.

38 changes: 38 additions & 0 deletions src/test/java/org/cactoos/func/SyncFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ public void funcWorksInThreads() {
MatcherAssert.assertThat(list.size(), Matchers.equalTo(threads));
}

@Test
public void procWorksInThreads() {
final int threads = 100;
final int[] counter = new int[]{0};
MatcherAssert.assertThat(
"Sync func with proc can't work well in multiple threads",
func -> func.apply(1),
new RunsInThreads<>(
new SyncFunc<Integer, Boolean>(
new ProcOf<>(
input -> counter[0] = counter[0] + input
),
true
),
threads
)
);
MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads));
}

@Test
public void callableWorksInThreads() {
final int threads = 100;
Expand All @@ -78,4 +98,22 @@ public void callableWorksInThreads() {
);
MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads));
}

@Test
public void runnableWorksInThreads() {
final int threads = 100;
final int[] counter = new int[]{0};
MatcherAssert.assertThat(
"Sync func with runnable can't work well in multiple threads",
func -> func.apply(1),
new RunsInThreads<>(
new SyncFunc<Integer, Boolean>(
() -> counter[0] = counter[0] + 1,
true
),
threads
)
);
MatcherAssert.assertThat(counter[0], Matchers.equalTo(threads));
}
}
82 changes: 0 additions & 82 deletions src/test/java/org/cactoos/func/SyncProcTest.java

This file was deleted.

0 comments on commit 293d4b2

Please sign in to comment.