//NG example
public class SynchronizedNotUse {
//Data shared by multiple threads
private int value = 0;
//Execution of 100,000 threads
public static void main(String[] args) {
final int TASK_NUM = 100000;
var th = new Thread[TASK_NUM];
var tb = new SynchronizedNotUse();
//Thread creation and execution
for (var i = 0; i < TASK_NUM; i++) {
th[i] = new Thread(() -> {
tb.increment();
});
th[i].start();
}
//Wait until the thread ends
for (var i = 0; i < TASK_NUM; i++) {
try {
th[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(tb.value); //Different every time
}
//Increment the value field
void increment() {
this.value++;
}
}
public class SynchronizedUse {
private int value = 0;
public static void main(String[] args) {
final int TASK_NUM = 100000;
var th = new Thread[TASK_NUM];
var tb = new SynchronizedUse();
for (var i = 0; i < TASK_NUM; i++) {
th[i] = new Thread(() -> {
tb.increment();
});
th[i].start();
}
for (var i = 0; i < TASK_NUM; i++) {
try {
th[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(tb.value);
}
//Specify the object to be locked as the current instance
void increment() {
synchronized(this) {
this.value++;
}
}
}
volatile
modifier can also be used for variable assignment / retrieval onlysynchronized void increment() {
this.value++;
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockBasic {
private int value = 0;
private final Lock lock = new ReentrantLock();
public static void main(String[] args) {
final int TASK_NUM = 100000;
var th = new Thread[TASK_NUM];
var tb = new LockBasic();
for (var i = 0; i < TASK_NUM; i++) {
th[i] = new Thread(() -> {
tb.increment();
});
th[i].start();
}
for (var i = 0; i < TASK_NUM; i++) {
try {
th[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(tb.value);
}
void increment() {
//Rock acquisition
lock.lock();
try {
this.value++;
} finally {
//unlock
lock.unlock();
}
}
}
if (lock.tryLock(10,TimeUnit.SECONDS)) {
try {
//Processing to be exclusively controlled
} finally {
lock.unlock();
}
}else{
//What to do if you can't get a lock
}
synchronized
and java.util.concurrent.locks
are exclusive control assuming lockimport java.util.concurrent.atomic.AtomicInteger;
public class AtomicBasic {
private AtomicInteger value = new AtomicInteger();
public static void main(String[] args) {
final int TASK_NUM = 100000;
var th = new Thread[TASK_NUM];
var tb = new AtomicBasic();
for (var i = 0; i < TASK_NUM; i++) {
th[i] = new Thread(() -> {
tb.increment();
});
th[i].start();
}
for (var i = 0; i < TASK_NUM; i++) {
try {
th[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(tb.value);
}
void increment() {
value.getAndIncrement();
}
}
public class ThreadPool implements Runnable {
@Override
public void run() {
for (var i = 0; i < 30; i++){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
import java.util.concurrent.Executors;
public class ThreadPoolBasic {
public static void main(String[] args) {
//Create a thread pool with 10 threads
var es = Executors.newFixedThreadPool(10);
//Call and execute with execute method
es.execute(new ThreadPool());
es.execute(new ThreadPool());
es.execute(new ThreadPool());
es.shutdown();
}
}
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadSchedule {
public static void main(String[] args) {
//Thread pool preparation
var sche = Executors.newScheduledThreadPool(2);
//Schedule execution registration
sche.scheduleAtFixedRate(() -> {
System.out.println(LocalDateTime.now());
}, 0, 5, TimeUnit.SECONDS);
//Pause the main thread after waiting for the schedule to execute
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Shut down the thread pool
sche.shutdown();
}
}
//Find a random number in another thread and pause for a few milliseconds
//Display that value in the main thread
import java.util.Random;
import java.util.concurrent.Callable;
//Callable<Integer>Assign Integer type with
public class ThreadCallable implements Callable<Integer> {
@Override
//Code to execute in the Callable interface
public Integer call() throws Exception {
var rnd = new Random();
var num = rnd.nextInt(1000);
Thread.sleep(num);
return num;
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
public class ThreadCallableBasic {
public static void main(String[] args) {
//Thread execution
var exe = Executors.newSingleThreadExecutor();
var r1 = exe.submit(new ThreadCallable());
var r2 = exe.submit(new ThreadCallable());
var r3 = exe.submit(new ThreadCallable());
//Thread result display
//The return value of the submit method is Future<Integer>
try {
System.out.println("r1: " + r1.get());
System.out.println("r2: " + r2.get());
System.out.println("r3: " + r3.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
exe.shutdown();
}
}
//Find a random number in another thread and pause for a few milliseconds
//Show its value
import java.util.Random;
import java.util.concurrent.CompletableFuture;
public class FutureBasic {
public static void main(String[] args) {
//Asynchronous processing execution
CompletableFuture.supplyAsync(() -> {
var r = new Random();
var num = r.nextInt(1000);
heavy(num);
return num;
})
//Processing after completion
.thenAcceptAsync((result) -> {
System.out.println(result);
});
System.out.println("...Any post-processing...");
heavy(7000);
}
//Dummy processing (heavy) suspends processing only for the specified time
public static void heavy(int num) {
try {
Thread.sleep(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Random;
import java.util.concurrent.CompletableFuture;
public class FutureComplete {
public static void main(String[] args) {
CompletableFuture.supplyAsync(() -> {
var r = new Random();
var num = r.nextInt(1000);
heavy(num);
return num;
})
.whenCompleteAsync((result, ex) -> {
//success
if (ex == null) {
System.out.println(result);
} else {
//Failure
System.out.println(ex.getMessage());
}
});
heavy(7000);
}
public static void heavy(int num) {
try {
Thread.sleep(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Random;
import java.util.concurrent.CompletableFuture;
public class FutureSeq {
public static void main(String[] args) {
//Process 1(Random number generation)
CompletableFuture.supplyAsync(() -> {
var r = new Random();
var num = r.nextInt(5000);
heavy(2000);
System.out.printf("Process 1: %d\n", num);
return num;
})
//Process 2(Random number times)
.thenApplyAsync((data) -> {
var result = data * 2;
heavy(2000);
System.out.printf("Process 2: %d\n", result);
return result;
})
//Process 3(Random number double)
.thenAcceptAsync((data) -> {
var num = data * 2;
heavy(2000);
System.out.printf("Process 3: %d\n", num);
});
heavy(7000);
}
public static void heavy(int num) {
try {
Thread.sleep(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Recommended Posts