Skip to content

Instantly share code, notes, and snippets.

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class ForkJoinSum extends RecursiveTask {
private final long[] numbers;
private final int start;
private final int end;
private static final int THRESHOLD = 10_000; // threshold for splitting
public ForkJoinSum(long[] numbers, int start, int end) {
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 1; i <= 5; i++) {
int taskId = i;
executor.submit(() -> {
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunnableExample {
public static void main(String[] args) {
// Create an ExecutorService with a single thread
ExecutorService executor = Executors.newSingleThreadExecutor();
// Create a Runnable task
Runnable task = () -> {
import java.util.concurrent.Callable;
public class CallableInThread {
public static void main(String[] args) throws InterruptedException {
Callable<String> callable = () -> {
Thread.sleep(1000);
return "Result from callable";
};
final String[] result = new String[1]; // Use shared container
import java.util.concurrent.Callable;
public class Main {
public static void main(String[] args) throws Exception {
Callable<String> task = () -> "Hello from Callable";
// Not running in a new thread — just calling the method directly
String result = task.call();
System.out.println(result);
}
import java.util.concurrent.*;
class MyCallable implements Callable<String> {
public String call() throws Exception {
return "Callable thread result";
}
}
public class Main {
public static void main(String[] args) throws Exception {
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running...");
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
t1.start(); // Starts a new thread
package com.cb.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
package com.cb.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.zalando.problem.Problem;
import org.zalando.problem.Status;
import java.net.URI;
import java.util.Map;