#502 Replaced usages of System.out with logger.
This commit is contained in:
@ -23,6 +23,9 @@
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -48,6 +51,8 @@ import java.util.stream.IntStream;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
@ -71,7 +76,7 @@ public class App {
|
||||
try {
|
||||
executeService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,9 @@
|
||||
*/
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
/**
|
||||
@ -29,6 +32,8 @@ import java.util.concurrent.locks.Lock;
|
||||
*/
|
||||
public class Reader implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Reader.class);
|
||||
|
||||
private Lock readLock;
|
||||
|
||||
private String name;
|
||||
@ -55,8 +60,8 @@ public class Reader implements Runnable {
|
||||
*
|
||||
*/
|
||||
public void read() throws InterruptedException {
|
||||
System.out.println(name + " begin");
|
||||
LOGGER.info("{} begin", name);
|
||||
Thread.sleep(250);
|
||||
System.out.println(name + " finish");
|
||||
LOGGER.info("{} finish", name);
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,9 @@
|
||||
*/
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
/**
|
||||
@ -29,6 +32,8 @@ import java.util.concurrent.locks.Lock;
|
||||
*/
|
||||
public class Writer implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Writer.class);
|
||||
|
||||
private Lock writeLock;
|
||||
|
||||
private String name;
|
||||
@ -55,8 +60,8 @@ public class Writer implements Runnable {
|
||||
* Simulate the write operation
|
||||
*/
|
||||
public void write() throws InterruptedException {
|
||||
System.out.println(name + " begin");
|
||||
LOGGER.info("{} begin", name);
|
||||
Thread.sleep(250);
|
||||
System.out.println(name + " finish");
|
||||
LOGGER.info("{} finish", name);
|
||||
}
|
||||
}
|
||||
|
@ -31,13 +31,15 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderAndWriterTest extends StdOutTest {
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderAndWriterTest.class);
|
||||
|
||||
/**
|
||||
* Verify reader and writer can only get the lock to read and write orderly
|
||||
@ -60,7 +62,7 @@ public class ReaderAndWriterTest extends StdOutTest {
|
||||
try {
|
||||
executeService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown", e);
|
||||
}
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
@ -91,7 +93,7 @@ public class ReaderAndWriterTest extends StdOutTest {
|
||||
try {
|
||||
executeService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown", e);
|
||||
}
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
|
@ -31,12 +31,16 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderTest extends StdOutTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderTest.class);
|
||||
|
||||
/**
|
||||
* Verify that multiple readers can get the read lock concurrently
|
||||
*/
|
||||
@ -57,7 +61,7 @@ public class ReaderTest extends StdOutTest {
|
||||
try {
|
||||
executeService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown", e);
|
||||
}
|
||||
|
||||
// Read operation will hold the read lock 250 milliseconds, so here we prove that multiple reads
|
||||
|
@ -31,12 +31,16 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class WriterTest extends StdOutTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WriterTest.class);
|
||||
|
||||
/**
|
||||
* Verify that multiple writers will get the lock in order.
|
||||
*/
|
||||
@ -58,7 +62,7 @@ public class WriterTest extends StdOutTest {
|
||||
try {
|
||||
executeService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Error waiting for ExecutorService shutdown");
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown", e);
|
||||
}
|
||||
// Write operation will hold the write lock 250 milliseconds, so here we verify that when two
|
||||
// writer execute concurrently, the second writer can only writes only when the first one is
|
||||
|
Reference in New Issue
Block a user