Skip to content

Commit

Permalink
adding code for BAEL-1262 (eugenp#2866)
Browse files Browse the repository at this point in the history
* adding code for BAEL-1262

* fixing the build issue by moving the test to test folder
  • Loading branch information
sanaulla123 authored and maibin committed Oct 25, 2017
1 parent 8248a66 commit 080af1c
Showing 1 changed file with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.baeldung.concurrent.runnable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.RandomUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RunnableVsThreadTest {

private static Logger log =
LoggerFactory.getLogger(RunnableVsThreadTest.class);

@Test
public void givenARunnable_whenRunIt_thenResult() throws Exception{
Thread thread = new Thread(new SimpleRunnable(
"SimpleRunnable executed using Thread"));
thread.start();
thread.join();

ExecutorService executorService =
Executors.newCachedThreadPool();

executorService.submit(new SimpleRunnable(
"SimpleRunnable executed using ExecutorService")).get();

executorService.submit(()->
log.info("Lambda runnable executed!!!")).get();
executorService.shutdown();
}

@Test
public void givenAThread_whenRunIt_thenResult() throws Exception{
Thread thread = new SimpleThread(
"SimpleThread executed using Thread");
thread.start();
thread.join();

ExecutorService executorService =
Executors.newCachedThreadPool();
executorService.submit(new SimpleThread(
"SimpleThread executed using ExecutorService")).get();
}

@Test
public void givenACallable_whenRunIt_thenResult() throws Exception {
ExecutorService executorService =
Executors.newCachedThreadPool();

Future<Integer> future = executorService.submit(new SimpleCallable());
log.info("Result from callable: {}", future.get());

future = executorService.submit(() -> {
return RandomUtils.nextInt(0, 100);
});
log.info("Result from callable: {}", future.get());

}
}

class SimpleThread extends Thread{

private static final Logger log =
LoggerFactory.getLogger(SimpleThread.class);

private String message;

public SimpleThread(String message) {
this.message = message;
}

@Override
public void run() {
log.info(message);
}
}

class SimpleRunnable implements Runnable {

private static final Logger log =
LoggerFactory.getLogger(SimpleRunnable.class);

private String message;

public SimpleRunnable(String message) {
this.message = message;
}


@Override
public void run() {
log.info(message);
}
}

class SimpleCallable implements Callable<Integer> {

@Override
public Integer call() throws Exception {
return RandomUtils.nextInt(0, 100);
}

}

0 comments on commit 080af1c

Please sign in to comment.