Java : Runnable with Examples
Runnable (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Runnable methods.
Summary
try (final var executor = Executors.newSingleThreadExecutor()) {
final var task = new Runnable() {
@Override
public void run() {
System.out.println("Run!");
}
};
System.out.println("-- submit --");
executor.submit(task);
}
// Result
// ↓
//-- submit --
//Run!
Methods
void run ()
final var runnable = new Runnable() {
@Override
public void run() {
System.out.println("Run!");
}
};
runnable.run(); // Run!
An example of lambda expression.
try (final var executor = Executors.newSingleThreadExecutor()) {
System.out.println("-- submit --");
executor.submit(() -> {
System.out.println("Run!");
});
}
// Result
// ↓
//-- submit --
//Run!
Related posts
Thread
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit
Functional interface