[BAEL-7074] Update tests

This commit is contained in:
Michael Pratt 2023-11-02 19:23:43 -06:00
parent 02e02f928f
commit 38f30e733f
3 changed files with 19 additions and 56 deletions

View File

@ -23,7 +23,6 @@ public class Employee {
private static void incrementCount() { private static void incrementCount() {
count.incrementAndGet(); count.incrementAndGet();
System.out.println("Count = " + count.get());
} }
public static int getCount() { public static int getCount() {

View File

@ -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;
}
}

View File

@ -5,13 +5,22 @@ import org.junit.Test;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; 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 public class SychronizeStaticDataUnitTest
{ {
private final Executor pool = Executors.newFixedThreadPool(4); private final Executor pool = Executors.newFixedThreadPool(4);
private final int numberToTest = 100;
@Test @Test
public void whenNotSynchronized_thenDataOutOfOrder() { public void whenNotSynchronized_thenDataOutOfOrder() {
int numberToTest = 100;
System.out.println("No synchronization");
for(int i = 0; i < numberToTest; i++) { for(int i = 0; i < numberToTest; i++) {
int finalI = 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 @Test
public void whenSynchronizedMethod_thenDataInOrder() { public void whenSynchronizedMethod_thenDataInOrder() {
int numberToTest = 100;
System.out.println("Synchronization with synchronized method");
for(int i = 0; i < numberToTest; i++) { for(int i = 0; i < numberToTest; i++) {
int finalI = i; int finalI = i;
@ -50,7 +47,8 @@ public class SychronizeStaticDataUnitTest
@Test @Test
public void whenSynchronizedBlock_thenDataInOrder() { public void whenSynchronizedBlock_thenDataInOrder() {
int numberToTest = 100;
System.out.println("Synchronization with synchronized block");
for(int i = 0; i < numberToTest; i++) { for(int i = 0; i < numberToTest; i++) {
int finalI = i; int finalI = i;
@ -63,20 +61,15 @@ public class SychronizeStaticDataUnitTest
@Test @Test
public void whenAtomicInteger_thenDataInOrder() { public void whenAtomicInteger_thenDataInOrder() {
int numberToTest = 100; // Not straight forward to test this because we cannot log/print
// and increment values in a synchronized fashion like other
for(int i = 0; i < numberToTest; i++) { // tests
int finalI = i;
pool.execute(() ->
{
new com.baeldung.concurrent.synchronizestatic.atomicinteger.Employee(finalI, "John", "Smith");
});
}
} }
@Test @Test
public void whenReentrantLock_thenDataInOrder() { public void whenReentrantLock_thenDataInOrder() {
int numberToTest = 100;
System.out.println("Synchronization with ReentrantLock");
for(int i = 0; i < numberToTest; i++) { for(int i = 0; i < numberToTest; i++) {
int finalI = i; int finalI = i;