Java
/
Threads
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Executor
ExecutorService allows you to run asynchron tasks without having to deal with threads directly.
/**
* Creating a thread is an expensive operation ...
* and it should be minimized.
*
* Having worker threads minimizes the overhead ...
* because executor service has to create the thread pool only once.
*
* In a fixed thread-pool, the executor service makes sure ...
* that the pool always has the specified number of threads running.
*/
package com.minte9.threads.scheduler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Executor {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
// fixed pool = 2 thread at the same time
Runnable task1 = () -> { // loop
while(true) {
System.out.println(
"Task 1 / " + Thread.currentThread().getName()
);
sleep(2);
}
};
Runnable task2 = () -> { // only once
System.out.println(
"Task 2 / " + Thread.currentThread().getName()
);
sleep(1);
};
Runnable task3 = () -> { // loop
while(true) {
System.out.println(
"Task 3 / " + Thread.currentThread().getName()
);
sleep(1);
}
};
service.submit(task1);
service.submit(task2);
service.submit(task3);
service.shutdown();
/*
Task 1 / pool-1-thread-1
Task 2 / pool-1-thread-2 - Look Here
Task 1 / pool-1-thread-1
Task 3 / pool-1-thread-2
Task 1 / pool-1-thread-1
Task 3 / pool-1-thread-2
...
*/
}
public static void sleep(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException ex) {}
}
}
Scheduled
ScheduledExecutorService can run a task perioadically (and after an initial delay).
/**
* ScheduledExecutorService can run a task perioadically ...
* or after an initial delay.
*/
package com.minte9.threads.scheduler;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Scheduled {
public static void main(String[] args)
throws InterruptedException {
// Create a service with 1 thread
ScheduledExecutorService service =
Executors.newScheduledThreadPool(1);
// Schedule a task to run repeatedly
service.scheduleAtFixedRate(() -> {
System.out.println(new Date() + " - scheduled");
}, 5L, 10L, TimeUnit.SECONDS); // 5s, 10s delay
// Main thread
while(true) {
System.out.println(new Date() + " - main");
TimeUnit.SECONDS.sleep(10); // 10S
}
/*
Tue Oct 05 17:54:13 EEST 2021 - main
Tue Oct 05 17:54:18 EEST 2021 - scheduled
Tue Oct 05 17:54:23 EEST 2021 - main
Tue Oct 05 17:54:28 EEST 2021 - scheduled
...
*/
}
}
➥ Questions