Add various gui samples
This commit is contained in:
44
async/src/main/java/asyncservice/ExampleService.java
Normal file
44
async/src/main/java/asyncservice/ExampleService.java
Normal file
@ -0,0 +1,44 @@
|
||||
package asyncservice;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class ExampleService {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
|
||||
|
||||
String work() {
|
||||
sleep(7000, TimeUnit.MILLISECONDS); /* Pretend to be busy... */
|
||||
char[] str = new char[5];
|
||||
ThreadLocalRandom current = ThreadLocalRandom.current();
|
||||
for (int idx = 0; idx < str.length; ++idx)
|
||||
str[idx] = (char) ('A' + current.nextInt(26));
|
||||
String msg = new String(str);
|
||||
log("Generated message: " + msg);
|
||||
return msg;
|
||||
}
|
||||
|
||||
private void sleep(long average, TimeUnit unit) {
|
||||
String name = Thread.currentThread().getName();
|
||||
long timeout = Math.min(exponential(average), Math.multiplyExact(10, average));
|
||||
log("{} sleeping {} {}...", name, timeout, unit);
|
||||
try {
|
||||
unit.sleep(timeout);
|
||||
log(name + " awoke.");
|
||||
} catch (InterruptedException abort) {
|
||||
Thread.currentThread().interrupt();
|
||||
log(name + " interrupted.");
|
||||
}
|
||||
}
|
||||
|
||||
private long exponential(long avg) {
|
||||
return (long) (avg * -Math.log(1 - ThreadLocalRandom.current().nextDouble()));
|
||||
}
|
||||
|
||||
private void log(String s, Object... o) {
|
||||
LOGGER.info(s, o);
|
||||
}
|
||||
}
|
49
async/src/main/java/asyncservice/Main.java
Normal file
49
async/src/main/java/asyncservice/Main.java
Normal file
@ -0,0 +1,49 @@
|
||||
package asyncservice;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
|
||||
|
||||
private static <T> Callable<T> lazyval(T val, long timeout) {
|
||||
return () -> {
|
||||
Thread.sleep(1000);
|
||||
log("Task completed with : " + val);
|
||||
return val;
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(4);
|
||||
final Future<Integer> asyncRes = executor.submit(lazyval(1, 100L));
|
||||
final Future<String> asyncRes1 = executor.submit(lazyval("Hello", 20L));
|
||||
final Future<String> asyncRes2 = executor.submit(lazyval("World", 100L));
|
||||
Thread.sleep(15000);
|
||||
log("End of executor example.");
|
||||
executor.shutdown();
|
||||
getTaskNotificationWithoutBlocking();
|
||||
}
|
||||
|
||||
private static void getTaskNotificationWithoutBlocking() throws InterruptedException {
|
||||
ExampleService svc = new ExampleService();
|
||||
CompletableFuture<String> future = CompletableFuture.supplyAsync(svc::work);
|
||||
Main listener = new Main();
|
||||
future.thenAccept(listener::notify);
|
||||
Thread.sleep(10000);
|
||||
log("Exciting main...");
|
||||
}
|
||||
|
||||
private void notify(String s) {
|
||||
log("Received message ", s);
|
||||
}
|
||||
|
||||
private static void log(String s, Object... o) {
|
||||
LOGGER.info(s, o);
|
||||
}
|
||||
|
||||
}
|
24
async/src/main/java/threadexamples/BasicThreadExample.java
Normal file
24
async/src/main/java/threadexamples/BasicThreadExample.java
Normal file
@ -0,0 +1,24 @@
|
||||
package threadexamples;
|
||||
|
||||
public class BasicThreadExample extends Thread {
|
||||
|
||||
private static int cpt = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int tmp = cpt;
|
||||
for (int i = 0; i < 1000000; i++);
|
||||
tmp++;
|
||||
cpt = tmp;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread thread1 = new BasicThreadExample();
|
||||
Thread thread2 = new BasicThreadExample();
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
System.out.println("Compteur : " + cpt);
|
||||
}
|
||||
}
|
40
async/src/main/java/threadexamples/producerconsumer/App.java
Normal file
40
async/src/main/java/threadexamples/producerconsumer/App.java
Normal file
@ -0,0 +1,40 @@
|
||||
package threadexamples.producerconsumer;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
BlockingQueue<Item> itemQueue = new LinkedBlockingQueue<>(5);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
final Producer producer = new Producer("Producer_" + i, itemQueue);
|
||||
executorService.submit(() -> {
|
||||
while (true) {
|
||||
producer.produce();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
final Consumer consumer = new Consumer("Consumer_" + i, itemQueue);
|
||||
executorService.submit(() -> {
|
||||
while(true) {
|
||||
consumer.consume();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
executorService.shutdown();
|
||||
|
||||
try {
|
||||
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
executorService.shutdownNow();
|
||||
} catch (InterruptedException e) {
|
||||
LoggerFactory.getLogger(App.class).error("Error waiting for ExecutorService shutdown");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package threadexamples.producerconsumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
public class Consumer {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);
|
||||
|
||||
private BlockingQueue<Item> itemQueue;
|
||||
private String name;
|
||||
private int itemId;
|
||||
|
||||
public Consumer(String name, BlockingQueue<Item> itemQueue) {
|
||||
this.itemQueue = itemQueue;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void consume() throws InterruptedException {
|
||||
final Item item = itemQueue.take();
|
||||
LOGGER.info("Consumer [{}] consume item [{}] produced by [{}]", name, item.getId(), item.getProducer());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package threadexamples.producerconsumer;
|
||||
|
||||
public class Item {
|
||||
|
||||
private String producer;
|
||||
private int id;
|
||||
|
||||
public Item(String producer, int id) {
|
||||
this.producer = producer;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getProducer() {
|
||||
return producer;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package threadexamples.producerconsumer;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
public class Producer {
|
||||
|
||||
private BlockingQueue<Item> itemQueue;
|
||||
private String name;
|
||||
private int itemId;
|
||||
|
||||
public Producer(String name, BlockingQueue itemQueue) {
|
||||
this.itemQueue = itemQueue;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void produce() throws InterruptedException {
|
||||
itemQueue.put(new Item(name, itemId++));
|
||||
Thread.sleep(new Random().nextInt(2000));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user