Merge pull request #9099 from alimate/BAEL-3954

Improving the What is Thread-Safety Article
This commit is contained in:
Eric Martin 2020-04-16 22:27:11 -05:00 committed by GitHub
commit 151cdf344f
4 changed files with 12 additions and 12 deletions

View File

@ -9,7 +9,7 @@ import com.baeldung.concurrent.threadsafety.callables.ReentranReadWriteLockCount
import com.baeldung.concurrent.threadsafety.callables.ReentrantLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.AtomicCounter;
import com.baeldung.concurrent.threadsafety.services.Counter;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import com.baeldung.concurrent.threadsafety.services.ObjectLockCounter;
import com.baeldung.concurrent.threadsafety.services.MessageService;
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
@ -48,9 +48,9 @@ public class Application {
System.out.println(future3.get());
System.out.println(future4.get());
ExtrinsicLockCounter extrinsicLockCounter = new ExtrinsicLockCounter();
Future<Integer> future5 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(extrinsicLockCounter));
Future<Integer> future6 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(extrinsicLockCounter));
ObjectLockCounter objectLockCounter = new ObjectLockCounter();
Future<Integer> future5 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(objectLockCounter));
Future<Integer> future6 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(objectLockCounter));
System.out.println(future5.get());
System.out.println(future6.get());

View File

@ -1,13 +1,13 @@
package com.baeldung.concurrent.threadsafety.callables;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import com.baeldung.concurrent.threadsafety.services.ObjectLockCounter;
import java.util.concurrent.Callable;
public class ExtrinsicLockCounterCallable implements Callable<Integer> {
private final ExtrinsicLockCounter counter;
private final ObjectLockCounter counter;
public ExtrinsicLockCounterCallable(ExtrinsicLockCounter counter) {
public ExtrinsicLockCounterCallable(ObjectLockCounter counter) {
this.counter = counter;
}

View File

@ -1,11 +1,11 @@
package com.baeldung.concurrent.threadsafety.services;
public class ExtrinsicLockCounter {
public class ObjectLockCounter {
private int counter;
private final Object lock = new Object();
public ExtrinsicLockCounter() {
public ObjectLockCounter() {
this.counter = 0;
}

View File

@ -1,7 +1,7 @@
package com.baeldung.concurrent.threadsafety;
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
import com.baeldung.concurrent.threadsafety.services.ObjectLockCounter;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
@ -10,12 +10,12 @@ import java.util.concurrent.Future;
import static org.assertj.core.api.Assertions.assertThat;
public class ExtrinsicLockCounterUnitTest {
public class ObjectLockCounterUnitTest {
@Test
public void whenCalledIncrementCounter_thenCorrect() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
ExtrinsicLockCounter counter = new ExtrinsicLockCounter();
ObjectLockCounter counter = new ObjectLockCounter();
Future<Integer> future1 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(counter));
Future<Integer> future2 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(counter));