formatting cleanup
This commit is contained in:
parent
f913859c6f
commit
4d8f92e587
|
@ -15,14 +15,12 @@ public class CyclicBarrierDemo {
|
||||||
private int NUM_PARTIAL_RESULTS;
|
private int NUM_PARTIAL_RESULTS;
|
||||||
private int NUM_WORKERS;
|
private int NUM_WORKERS;
|
||||||
|
|
||||||
|
|
||||||
private void runSimulation(int numWorkers, int numberOfPartialResults) {
|
private void runSimulation(int numWorkers, int numberOfPartialResults) {
|
||||||
NUM_PARTIAL_RESULTS = numberOfPartialResults;
|
NUM_PARTIAL_RESULTS = numberOfPartialResults;
|
||||||
NUM_WORKERS = numWorkers;
|
NUM_WORKERS = numWorkers;
|
||||||
|
|
||||||
cyclicBarrier = new CyclicBarrier(NUM_WORKERS, new AggregatorThread());
|
cyclicBarrier = new CyclicBarrier(NUM_WORKERS, new AggregatorThread());
|
||||||
System.out.println("Spawning " + NUM_WORKERS + " worker threads to compute "
|
System.out.println("Spawning " + NUM_WORKERS + " worker threads to compute " + NUM_PARTIAL_RESULTS + " partial results each");
|
||||||
+ NUM_PARTIAL_RESULTS + " partial results each");
|
|
||||||
for (int i = 0; i < NUM_WORKERS; i++) {
|
for (int i = 0; i < NUM_WORKERS; i++) {
|
||||||
Thread worker = new Thread(new NumberCruncherThread());
|
Thread worker = new Thread(new NumberCruncherThread());
|
||||||
worker.setName("Thread " + i);
|
worker.setName("Thread " + i);
|
||||||
|
@ -38,8 +36,7 @@ public class CyclicBarrierDemo {
|
||||||
List<Integer> partialResult = new ArrayList<>();
|
List<Integer> partialResult = new ArrayList<>();
|
||||||
for (int i = 0; i < NUM_PARTIAL_RESULTS; i++) {
|
for (int i = 0; i < NUM_PARTIAL_RESULTS; i++) {
|
||||||
Integer num = random.nextInt(10);
|
Integer num = random.nextInt(10);
|
||||||
System.out.println(thisThreadName
|
System.out.println(thisThreadName + ": Crunching some numbers! Final result - " + num);
|
||||||
+ ": Crunching some numbers! Final result - " + num);
|
|
||||||
partialResult.add(num);
|
partialResult.add(num);
|
||||||
}
|
}
|
||||||
partialResults.add(partialResult);
|
partialResults.add(partialResult);
|
||||||
|
@ -57,13 +54,12 @@ public class CyclicBarrierDemo {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
String thisThreadName = Thread.currentThread().getName();
|
String thisThreadName = Thread.currentThread().getName();
|
||||||
System.out.println(thisThreadName + ": Computing final sum of " + NUM_WORKERS
|
System.out.println(thisThreadName + ": Computing final sum of " + NUM_WORKERS + " workers, having " + NUM_PARTIAL_RESULTS + " results each.");
|
||||||
+ " workers, having " + NUM_PARTIAL_RESULTS + " results each.");
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (List<Integer> threadResult : partialResults) {
|
for (List<Integer> threadResult : partialResults) {
|
||||||
System.out.print("Adding ");
|
System.out.print("Adding ");
|
||||||
for (Integer partialResult : threadResult) {
|
for (Integer partialResult : threadResult) {
|
||||||
System.out.print(partialResult+" ");
|
System.out.print(partialResult + " ");
|
||||||
sum += partialResult;
|
sum += partialResult;
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
|
@ -15,7 +15,8 @@ public class Philosopher implements Runnable {
|
||||||
Thread.sleep(((int) (Math.random() * 100)));
|
Thread.sleep(((int) (Math.random() * 100)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void run() {
|
@Override
|
||||||
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
doAction(System.nanoTime() + ": Thinking"); // thinking
|
doAction(System.nanoTime() + ": Thinking"); // thinking
|
||||||
|
|
|
@ -6,22 +6,22 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class ExecutorServiceDemo {
|
public class ExecutorServiceDemo {
|
||||||
|
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(10);
|
ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||||
|
|
||||||
public void execute() {
|
public void execute() {
|
||||||
|
|
||||||
executor.submit(() -> {
|
executor.submit(() -> {
|
||||||
new Task();
|
new Task();
|
||||||
});
|
});
|
||||||
|
|
||||||
executor.shutdown();
|
executor.shutdown();
|
||||||
executor.shutdownNow();
|
executor.shutdownNow();
|
||||||
try {
|
try {
|
||||||
executor.awaitTermination(20l, TimeUnit.NANOSECONDS);
|
executor.awaitTermination(20l, TimeUnit.NANOSECONDS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,36 +9,36 @@ import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class FutureDemo {
|
public class FutureDemo {
|
||||||
|
|
||||||
public String invoke() {
|
public String invoke() {
|
||||||
|
|
||||||
String str = null;
|
String str = null;
|
||||||
|
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(10);
|
ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||||
|
|
||||||
Future<String> future = executorService.submit(() -> {
|
Future<String> future = executorService.submit(() -> {
|
||||||
// Task
|
// Task
|
||||||
Thread.sleep(10000l);
|
Thread.sleep(10000l);
|
||||||
return "Hellow world";
|
return "Hellow world";
|
||||||
});
|
});
|
||||||
|
|
||||||
future.cancel(false);
|
future.cancel(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
future.get(20, TimeUnit.SECONDS);
|
future.get(20, TimeUnit.SECONDS);
|
||||||
} catch (InterruptedException | ExecutionException | TimeoutException e1) {
|
} catch (InterruptedException | ExecutionException | TimeoutException e1) {
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (future.isDone() && !future.isCancelled()) {
|
if (future.isDone() && !future.isCancelled()) {
|
||||||
try {
|
try {
|
||||||
str = future.get();
|
str = future.get();
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,20 +4,20 @@ import java.util.concurrent.ThreadFactory;
|
||||||
|
|
||||||
public class BaeldungThreadFactory implements ThreadFactory {
|
public class BaeldungThreadFactory implements ThreadFactory {
|
||||||
|
|
||||||
private int threadId;
|
private int threadId;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public BaeldungThreadFactory(String name) {
|
public BaeldungThreadFactory(String name) {
|
||||||
threadId = 1;
|
threadId = 1;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Thread newThread(Runnable r) {
|
public Thread newThread(Runnable r) {
|
||||||
Thread t = new Thread(r, name + "-Thread_" + threadId);
|
Thread t = new Thread(r, name + "-Thread_" + threadId);
|
||||||
System.out.println("created new thread with id : " + threadId + " and name : " + t.getName());
|
System.out.println("created new thread with id : " + threadId + " and name : " + t.getName());
|
||||||
threadId++;
|
threadId++;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@ package com.baeldung.concurrent.threadfactory;
|
||||||
|
|
||||||
public class Task implements Runnable {
|
public class Task implements Runnable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// task details
|
// task details
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,26 +14,25 @@ public class LookupFSJNDI {
|
||||||
super();
|
super();
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() throws NamingException {
|
private void init() throws NamingException {
|
||||||
Hashtable<String, String> env = new Hashtable<String, String>();
|
Hashtable<String, String> env = new Hashtable<String, String>();
|
||||||
|
|
||||||
env.put (Context.INITIAL_CONTEXT_FACTORY,
|
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
|
||||||
"com.sun.jndi.fscontext.RefFSContextFactory");
|
|
||||||
// URI to namespace (actual directory)
|
// URI to namespace (actual directory)
|
||||||
env.put(Context.PROVIDER_URL, "file:./src/test/resources");
|
env.put(Context.PROVIDER_URL, "file:./src/test/resources");
|
||||||
|
|
||||||
ctx = new InitialContext(env);
|
ctx = new InitialContext(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
public InitialContext getCtx() {
|
public InitialContext getCtx() {
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getFile(String fileName) {
|
public File getFile(String fileName) {
|
||||||
File file;
|
File file;
|
||||||
try {
|
try {
|
||||||
file = (File)getCtx().lookup(fileName);
|
file = (File) getCtx().lookup(fileName);
|
||||||
} catch (NamingException e) {
|
} catch (NamingException e) {
|
||||||
file = null;
|
file = null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,13 +18,16 @@ public class User {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (o == null) return false;
|
return true;
|
||||||
if (this.getClass() != o.getClass()) return false;
|
if (o == null)
|
||||||
|
return false;
|
||||||
|
if (this.getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
User user = (User) o;
|
User user = (User) o;
|
||||||
return id != user.id && (!name.equals(user.name) && !email.equals(user.email));
|
return id != user.id && (!name.equals(user.name) && !email.equals(user.email));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 7;
|
int hash = 7;
|
||||||
|
|
|
@ -10,7 +10,6 @@ public class JMXTutorialMainlauncher {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(JMXTutorialMainlauncher.class);
|
private static final Logger LOG = LoggerFactory.getLogger(JMXTutorialMainlauncher.class);
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,8 @@ import java.net.*;
|
||||||
|
|
||||||
public class EchoClient {
|
public class EchoClient {
|
||||||
|
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(EchoClient.class);
|
private static final Logger LOG = LoggerFactory.getLogger(EchoClient.class);
|
||||||
|
|
||||||
private Socket clientSocket;
|
private Socket clientSocket;
|
||||||
private PrintWriter out;
|
private PrintWriter out;
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.baeldung.stream;
|
package com.baeldung.stream;
|
||||||
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -12,15 +12,10 @@ class StringHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
static String removeLastCharOptional(String s) {
|
static String removeLastCharOptional(String s) {
|
||||||
return Optional.ofNullable(s)
|
return Optional.ofNullable(s).filter(str -> str.length() != 0).map(str -> str.substring(0, str.length() - 1)).orElse(s);
|
||||||
.filter(str -> str.length() != 0)
|
|
||||||
.map(str -> str.substring(0, str.length() - 1))
|
|
||||||
.orElse(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static String removeLastCharRegexOptional(String s) {
|
static String removeLastCharRegexOptional(String s) {
|
||||||
return Optional.ofNullable(s)
|
return Optional.ofNullable(s).map(str -> str.replaceAll(".$", "")).orElse(s);
|
||||||
.map(str -> str.replaceAll(".$", ""))
|
|
||||||
.orElse(s);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,11 +25,7 @@ public class MyTokenizer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getTokensWithCollection(String str) {
|
public List<String> getTokensWithCollection(String str) {
|
||||||
return Collections
|
return Collections.list(new StringTokenizer(str, ",")).stream().map(token -> (String) token).collect(Collectors.toList());
|
||||||
.list(new StringTokenizer(str, ","))
|
|
||||||
.stream()
|
|
||||||
.map(token -> (String) token)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getTokensFromFile(String path, String delim) {
|
public List<String> getTokensFromFile(String path, String delim) {
|
||||||
|
|
|
@ -11,12 +11,12 @@ public class CustomTemporalAdjuster implements TemporalAdjuster {
|
||||||
@Override
|
@Override
|
||||||
public Temporal adjustInto(Temporal temporal) {
|
public Temporal adjustInto(Temporal temporal) {
|
||||||
switch (DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK))) {
|
switch (DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK))) {
|
||||||
case FRIDAY:
|
case FRIDAY:
|
||||||
return temporal.plus(3, ChronoUnit.DAYS);
|
return temporal.plus(3, ChronoUnit.DAYS);
|
||||||
case SATURDAY:
|
case SATURDAY:
|
||||||
return temporal.plus(2, ChronoUnit.DAYS);
|
return temporal.plus(2, ChronoUnit.DAYS);
|
||||||
default:
|
default:
|
||||||
return temporal.plus(1, ChronoUnit.DAYS);
|
return temporal.plus(1, ChronoUnit.DAYS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||||
public class Consumer implements Runnable {
|
public class Consumer implements Runnable {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Consumer.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Consumer.class);
|
||||||
|
|
||||||
|
|
||||||
private final TransferQueue<String> transferQueue;
|
private final TransferQueue<String> transferQueue;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int numberOfMessagesToConsume;
|
private final int numberOfMessagesToConsume;
|
||||||
|
|
|
@ -15,28 +15,24 @@ public class LongAccumulatorUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException {
|
public void givenLongAccumulator_whenApplyActionOnItFromMultipleThrads_thenShouldProduceProperResult() throws InterruptedException {
|
||||||
//given
|
// given
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(8);
|
ExecutorService executorService = Executors.newFixedThreadPool(8);
|
||||||
LongBinaryOperator sum = Long::sum;
|
LongBinaryOperator sum = Long::sum;
|
||||||
LongAccumulator accumulator = new LongAccumulator(sum, 0L);
|
LongAccumulator accumulator = new LongAccumulator(sum, 0L);
|
||||||
int numberOfThreads = 4;
|
int numberOfThreads = 4;
|
||||||
int numberOfIncrements = 100;
|
int numberOfIncrements = 100;
|
||||||
|
|
||||||
//when
|
// when
|
||||||
Runnable accumulateAction = () -> IntStream
|
Runnable accumulateAction = () -> IntStream.rangeClosed(0, numberOfIncrements).forEach(accumulator::accumulate);
|
||||||
.rangeClosed(0, numberOfIncrements)
|
|
||||||
.forEach(accumulator::accumulate);
|
|
||||||
|
|
||||||
for (int i = 0; i < numberOfThreads; i++) {
|
for (int i = 0; i < numberOfThreads; i++) {
|
||||||
executorService.execute(accumulateAction);
|
executorService.execute(accumulateAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// then
|
||||||
//then
|
|
||||||
executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
|
executorService.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
assertEquals(accumulator.get(), 20200);
|
assertEquals(accumulator.get(), 20200);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class ApplicationTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void main_NoInputState_TextPrintedToConsole() throws Exception {
|
public void main_NoInputState_TextPrintedToConsole() throws Exception {
|
||||||
Application.main(new String[]{});
|
Application.main(new String[] {});
|
||||||
assertEquals("User found in the collection", outContent.toString());
|
assertEquals("User found in the collection", outContent.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,7 +15,6 @@ public class LambdaExceptionWrappersUnitTest {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(LambdaExceptionWrappersUnitTest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(LambdaExceptionWrappersUnitTest.class);
|
||||||
|
|
||||||
|
|
||||||
private List<Integer> integers;
|
private List<Integer> integers;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|
|
@ -29,12 +29,9 @@ public class ListOfListsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListOfLists_thenCheckNames() {
|
public void givenListOfLists_thenCheckNames() {
|
||||||
assertEquals("Pen 1", ((Pen) listOfLists.get(0)
|
assertEquals("Pen 1", ((Pen) listOfLists.get(0).get(0)).getName());
|
||||||
.get(0)).getName());
|
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1).get(0)).getName());
|
||||||
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1)
|
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2).get(0)).getName());
|
||||||
.get(0)).getName());
|
|
||||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2)
|
|
||||||
.get(0)).getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
@ -43,11 +40,9 @@ public class ListOfListsUnitTest {
|
||||||
|
|
||||||
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
|
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
|
||||||
listOfLists.remove(1);
|
listOfLists.remove(1);
|
||||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1)
|
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1).get(0)).getName());
|
||||||
.get(0)).getName());
|
|
||||||
listOfLists.remove(0);
|
listOfLists.remove(0);
|
||||||
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0)
|
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0).get(0)).getName());
|
||||||
.get(0)).getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -67,11 +62,8 @@ public class ListOfListsUnitTest {
|
||||||
list.add(pencils);
|
list.add(pencils);
|
||||||
list.add(rubbers);
|
list.add(rubbers);
|
||||||
|
|
||||||
assertEquals("Pen 1", ((Pen) list.get(0)
|
assertEquals("Pen 1", ((Pen) list.get(0).get(0)).getName());
|
||||||
.get(0)).getName());
|
assertEquals("Pencil 1", ((Pencil) list.get(1).get(0)).getName());
|
||||||
assertEquals("Pencil 1", ((Pencil) list.get(1)
|
assertEquals("Rubber 1", ((Rubber) list.get(2).get(0)).getName());
|
||||||
.get(0)).getName());
|
|
||||||
assertEquals("Rubber 1", ((Rubber) list.get(2)
|
|
||||||
.get(0)).getName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ public class CoreThreadPoolIntegrationTest {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(CoreThreadPoolIntegrationTest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(CoreThreadPoolIntegrationTest.class);
|
||||||
|
|
||||||
|
|
||||||
@Test(timeout = 1000)
|
@Test(timeout = 1000)
|
||||||
public void whenCallingExecuteWithRunnable_thenRunnableIsExecuted() throws InterruptedException {
|
public void whenCallingExecuteWithRunnable_thenRunnableIsExecuted() throws InterruptedException {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue