[BAEL-7074] Update tests
This commit is contained in:
parent
02e02f928f
commit
38f30e733f
|
@ -23,7 +23,6 @@ public class Employee {
|
|||
|
||||
private static void incrementCount() {
|
||||
count.incrementAndGet();
|
||||
System.out.println("Count = " + count.get());
|
||||
}
|
||||
|
||||
public static int getCount() {
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
package com.baeldung.concurrent.synchronizestatic.volatilekeyword;
|
||||
|
||||
/**
|
||||
* No synchronization.
|
||||
*/
|
||||
public class Employee
|
||||
{
|
||||
|
||||
volatile static int count;
|
||||
int id;
|
||||
String name;
|
||||
String title;
|
||||
|
||||
public Employee(int id, String name, String title)
|
||||
{
|
||||
incrementCount();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
private static void incrementCount() {
|
||||
System.out.println("Count = " + ++count);
|
||||
}
|
||||
|
||||
public static Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
}
|
|
@ -5,13 +5,22 @@ import org.junit.Test;
|
|||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* The tests in this class show the output of creating multiple
|
||||
* types of Employee classes in the <code>synchronizedstatic</code>
|
||||
* package. When not synchronized the out will not be sequential;
|
||||
* when it is synchronized the output will be in sequential.
|
||||
*/
|
||||
public class SychronizeStaticDataUnitTest
|
||||
{
|
||||
private final Executor pool = Executors.newFixedThreadPool(4);
|
||||
|
||||
private final int numberToTest = 100;
|
||||
|
||||
@Test
|
||||
public void whenNotSynchronized_thenDataOutOfOrder() {
|
||||
int numberToTest = 100;
|
||||
|
||||
System.out.println("No synchronization");
|
||||
|
||||
for(int i = 0; i < numberToTest; i++) {
|
||||
int finalI = i;
|
||||
|
@ -22,22 +31,10 @@ public class SychronizeStaticDataUnitTest
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVolatile_thenDataInOrder() {
|
||||
int numberToTest = 100;
|
||||
|
||||
for(int i = 0; i < numberToTest; i++) {
|
||||
int finalI = i;
|
||||
pool.execute(() ->
|
||||
{
|
||||
new com.baeldung.concurrent.synchronizestatic.volatilekeyword.Employee(finalI, "John", "Smith");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSynchronizedMethod_thenDataInOrder() {
|
||||
int numberToTest = 100;
|
||||
|
||||
System.out.println("Synchronization with synchronized method");
|
||||
|
||||
for(int i = 0; i < numberToTest; i++) {
|
||||
int finalI = i;
|
||||
|
@ -50,7 +47,8 @@ public class SychronizeStaticDataUnitTest
|
|||
|
||||
@Test
|
||||
public void whenSynchronizedBlock_thenDataInOrder() {
|
||||
int numberToTest = 100;
|
||||
|
||||
System.out.println("Synchronization with synchronized block");
|
||||
|
||||
for(int i = 0; i < numberToTest; i++) {
|
||||
int finalI = i;
|
||||
|
@ -63,20 +61,15 @@ public class SychronizeStaticDataUnitTest
|
|||
|
||||
@Test
|
||||
public void whenAtomicInteger_thenDataInOrder() {
|
||||
int numberToTest = 100;
|
||||
|
||||
for(int i = 0; i < numberToTest; i++) {
|
||||
int finalI = i;
|
||||
pool.execute(() ->
|
||||
{
|
||||
new com.baeldung.concurrent.synchronizestatic.atomicinteger.Employee(finalI, "John", "Smith");
|
||||
});
|
||||
}
|
||||
// Not straight forward to test this because we cannot log/print
|
||||
// and increment values in a synchronized fashion like other
|
||||
// tests
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReentrantLock_thenDataInOrder() {
|
||||
int numberToTest = 100;
|
||||
|
||||
System.out.println("Synchronization with ReentrantLock");
|
||||
|
||||
for(int i = 0; i < numberToTest; i++) {
|
||||
int finalI = i;
|
||||
|
|
Loading…
Reference in New Issue