跳到主要内容

16 篇博文 含有标签「best-practices」

查看所有标签

Best Practices of kubectl

· 阅读需 6 分钟

Best Practices of Using kubectl to Analyze Kubernetes Cluster Status

Introduction​

Monitoring and analyzing the status of a Kubernetes (K8s) cluster is crucial for maintaining the health and efficiency of your applications. The kubectl command-line tool provides powerful capabilities for interacting with and analyzing your Kubernetes cluster. This article provides a comprehensive guide to the best practices of using kubectl to analyze cluster status, covering various scenarios such as checking cluster information, monitoring resources, troubleshooting issues, and performing advanced operations.


Basic Cluster Information​

Get Cluster Info​

To get basic information about the cluster, use the kubectl cluster-info command:

kubectl cluster-info

This command displays the Kubernetes master and DNS endpoints.

List Nodes​

To list all nodes in the cluster, use the kubectl get nodes command:

kubectl get nodes

This command displays the status, roles, age, and version of each node.

List Namespaces​

To list all namespaces in the cluster, use the kubectl get namespaces command:

kubectl get namespaces

This command displays the name and status of each namespace.


Resource Monitoring​

List Pods​

To list all pods in a namespace, use the kubectl get pods command:

kubectl get pods -n <namespace>

This command displays the name, ready status, status, restarts, and age of each pod.

List Deployments​

To list all deployments in a namespace, use the kubectl get deployments command:

kubectl get deployments -n <namespace>

This command displays the name, desired replicas, current replicas, up-to-date replicas, and available replicas of each deployment.

List Services​

To list all services in a namespace, use the kubectl get services command:

kubectl get services -n <namespace>

This command displays the name, type, cluster IP, external IP, and ports of each service.

List ConfigMaps​

To list all ConfigMaps in a namespace, use the kubectl get configmaps command:

kubectl get configmaps -n <namespace>

This command displays the name and age of each ConfigMap.

List Secrets​

To list all Secrets in a namespace, use the kubectl get secrets command:

kubectl get secrets -n <namespace>

This command displays the name and age of each Secret.


Advanced Resource Analysis​

Describe Resources​

To get detailed information about a specific resource, use the kubectl describe command:

kubectl describe <resource> <name> -n <namespace>

For example, to describe a pod:

kubectl describe pod <pod-name> -n <namespace>

This command provides detailed information about the resource, including events and status.

Logs​

To view the logs of a pod, use the kubectl logs command:

kubectl logs <pod-name> -n <namespace>

To follow the logs in real-time, use the -f flag:

kubectl logs -f <pod-name> -n <namespace>

Exec into Pod​

To execute a command inside a running pod, use the kubectl exec command:

kubectl exec -it <pod-name> -n <namespace> -- /bin/bash

This command opens an interactive shell inside the pod.

Port Forwarding​

To forward a local port to a port on a pod, use the kubectl port-forward command:

kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace>

This command allows you to access the pod's service locally.


Troubleshooting​

Check Pod Status​

To check the status of a specific pod, use the kubectl get pods command:

kubectl get pods <pod-name> -n <namespace>

This command displays the status and other details of the pod.

Check Node Status​

To check the status of a specific node, use the kubectl get nodes command:

kubectl get nodes <node-name>

This command displays the status and other details of the node.

Check Events​

To check recent events in the cluster, use the kubectl get events command:

kubectl get events -n <namespace>

This command displays recent events, which can help diagnose issues.

Check Resource Limits​

To check resource limits and requests for a pod, use the kubectl describe command:

kubectl describe pod <pod-name> -n <namespace>

This command provides detailed information about resource limits and requests.


Advanced Scenarios​

Scaling Deployments​

To scale a deployment, use the kubectl scale command:

kubectl scale deployment <deployment-name> --replicas=<number> -n <namespace>

This command scales the deployment to the specified number of replicas.

Rolling Updates​

To perform a rolling update on a deployment, use the kubectl rollout command:

kubectl rollout restart deployment <deployment-name> -n <namespace>

This command restarts the deployment, performing a rolling update.

Debugging with kubectl​

To debug a pod, use the kubectl debug command:

kubectl debug <pod-name> -n <namespace>

This command creates a debugging container inside the pod.


Conclusion​

Monitoring and analyzing the status of a Kubernetes cluster is essential for maintaining the health and efficiency of your applications. By mastering the various options and scenarios covered in this article, you can effectively use kubectl to interact with and analyze your Kubernetes cluster.


References​

  1. kubectl Cheat Sheet
  2. kubectl Command Reference
  3. Kubernetes Documentation
  4. Kubernetes Best Practices

By following these best practices, you can leverage kubectl to its full potential and efficiently manage your Kubernetes cluster monitoring and troubleshooting tasks.

Best Practices of tcpdump and netstat

· 阅读需 4 分钟

Introduction​

Network analysis is a critical skill for system administrators and network engineers. Tools like tcpdump, netstat, ping, traceroute, nmap, and iftop provide powerful capabilities for monitoring, diagnosing, and troubleshooting network issues. This article provides a comprehensive guide to the best practices of using these commands, covering various scenarios such as capturing and filtering network traffic, displaying network statistics, and diagnosing network issues.


tcpdump​

Basic Usage​

To start capturing network traffic, use the following command:

tcpdump

This command captures all traffic on the default network interface.

Filtering Traffic​

To filter traffic by protocol, use the -i option:

tcpdump -i eth0

To filter traffic by IP address, use the host keyword:

tcpdump host 192.168.1.1

To filter traffic by port, use the port keyword:

tcpdump port 80

Saving Captures​

To save captured traffic to a file, use the -w option:

tcpdump -w capture.pcap

Reading Captures​

To read a saved capture file, use the -r option:

tcpdump -r capture.pcap

netstat​

Basic Usage​

To display all active connections, use the following command:

netstat -a

Displaying Active Connections​

To display active TCP connections, use the -t option:

netstat -at

Displaying Listening Ports​

To display listening ports, use the -l option:

netstat -lt

Displaying Network Statistics​

To display network statistics, use the -s option:

netstat -s

Other Network Analysis Commands​

ping​

To check connectivity to a host, use the ping command:

ping google.com

traceroute​

To trace the route to a host, use the traceroute command:

traceroute google.com

nmap​

To scan for open ports on a host, use the nmap command:

nmap 192.168.1.1

iftop​

To monitor network traffic in real-time, use the iftop command:

iftop -i eth0

Advanced Scenarios​

Analyzing HTTP Traffic​

To capture and analyze HTTP traffic, use tcpdump with port filtering:

tcpdump -i eth0 port 80

Monitoring Real-Time Traffic​

To monitor real-time traffic on a specific interface, use iftop:

iftop -i eth0

Diagnosing Network Issues​

To diagnose network issues, use a combination of ping, traceroute, and tcpdump:

ping google.com
traceroute google.com
tcpdump -i eth0 host google.com

Conclusion​

Network analysis is a critical skill for maintaining and troubleshooting network infrastructure. By mastering the various options and scenarios covered in this article, you can effectively use tools like tcpdump, netstat, ping, traceroute, nmap, and iftop to monitor, diagnose, and troubleshoot network issues.


References​

  1. tcpdump Man Page
  2. netstat Man Page
  3. ping Man Page
  4. traceroute Man Page
  5. nmap Man Page
  6. iftop Man Page

By following these best practices, you can leverage these network analysis tools to their full potential and efficiently manage your network monitoring and troubleshooting tasks.

Best Practices of Deploying PostgreSQL in Kubernetes

· 阅读需 3 分钟

This guide walks through deploying PostgreSQL in a Kubernetes Pod with a sidecar container that collects PostgreSQL logs and forwards them to Elasticsearch. We’ll use Helm to manage deployments, including setting up necessary ConfigMaps and Secrets for configuration.

Project Structure​

postgresql-logging/
├── charts/
│ └── postgresql-log-sidecar/
│ ├── Chart.yaml
│ ├── templates/
│ │ ├── configmap.yaml
│ │ ├── deployment.yaml
│ │ ├── secret.yaml
│ │ ├── service.yaml
│ ├── values.yaml
├── README.md

1. Architecture Overview​

The following diagram shows how the PostgreSQL Pod and the sidecar interact for log collection:

+-----------------------------------+
| PostgreSQL Pod |
| |
| +-----------------------------+ |
| | PostgreSQL Container | |
| | - Exposes Port 5432 | |
| +-----------------------------+ |
| |
| +-----------------------------+ |
| | Log Collector | |
| | - Collects Logs | |
| | - Pushes to Elasticsearch | |
| +-----------------------------+ |
+-----------------------------------+

2. Helm Chart Configuration​

Chart.yaml​

apiVersion: v2
name: postgresql-log-sidecar
description: A Helm chart for PostgreSQL with log sidecar
version: 0.1.0
appVersion: "13"

values.yaml​

Define values for PostgreSQL configuration and the sidecar container.

postgresql:
username: admin
password: password123
database: appdb

elasticsearch:
host: "elasticsearch:9200"
index: "postgresql-logs"

sidecar:
image: fluent/fluent-bit
tag: "1.8"
resources:
limits:
memory: "200Mi"
requests:
memory: "100Mi"

3. ConfigMap and Secret​

configmap.yaml​

This ConfigMap includes log configuration for the sidecar container.

apiVersion: v1
kind: ConfigMap
metadata:
name: postgresql-log-config
data:
fluent-bit.conf: |
[SERVICE]
Flush 1
Log_Level info

[INPUT]
Name tail
Path /var/log/postgresql/postgresql.log
Tag postgresql

[OUTPUT]
Name es
Match *
Host {{ .Values.elasticsearch.host }}
Port 9200
Index {{ .Values.elasticsearch.index }}

secret.yaml​

Create a Secret for PostgreSQL credentials.

apiVersion: v1
kind: Secret
metadata:
name: postgresql-secret
type: Opaque
data:
postgresql-username: {{ .Values.postgresql.username | b64enc }}
postgresql-password: {{ .Values.postgresql.password | b64enc }}

4. Deployment Configuration​

deployment.yaml​

The main deployment file sets up PostgreSQL and the Fluent Bit sidecar container.

apiVersion: apps/v1
kind: Deployment
metadata:
name: postgresql-log-sidecar
spec:
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
containers:
- name: postgresql
image: postgres:13
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: {{ .Values.postgresql.database }}
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: postgresql-secret
key: postgresql-username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgresql-secret
key: postgresql-password
volumeMounts:
- name: postgres-logs
mountPath: /var/log/postgresql
resources:
requests:
memory: "500Mi"
limits:
memory: "1Gi"

- name: log-collector
image: {{ .Values.sidecar.image }}:{{ .Values.sidecar.tag }}
args: ["-c", "/fluent-bit/etc/fluent-bit.conf"]
volumeMounts:
- name: postgres-logs
mountPath: /var/log/postgresql
- name: config
mountPath: /fluent-bit/etc
subPath: fluent-bit.conf
resources:
limits:
memory: {{ .Values.sidecar.resources.limits.memory }}
requests:
memory: {{ .Values.sidecar.resources.requests.memory }}
volumes:
- name: postgres-logs
emptyDir: {}
- name: config
configMap:
name: postgresql-log-config

service.yaml​

Create a Service to expose PostgreSQL internally within the cluster.

apiVersion: v1
kind: Service
metadata:
name: postgresql
spec:
type: ClusterIP
ports:
- port: 5432
targetPort: 5432
selector:
app: postgresql

5. Deploying the Helm Chart​

Install the chart with:

helm install postgresql-log-sidecar ./charts/postgresql-log-sidecar

Programming Standards of Java Concurrency

· 阅读需 5 分钟

Thread safety is a critical aspect of building robust and scalable Java applications. Ensuring that your application handles concurrent operations correctly can prevent many common issues such as race conditions, deadlocks, and data inconsistencies. This article provides best practices and detailed guidelines for achieving thread safety in Java applications.


1. Thread Safety in Singleton Objects

  • Requirement: Ensure that singleton objects and their methods are thread-safe.
  • Explanation: Resource-driven classes, utility classes, and singleton factory classes must be thread-safe to prevent race conditions.

2. Meaningful Thread Naming

  • Requirement: Assign meaningful names to threads and thread pools for easier debugging.
  • Example: Custom thread factory with grouping based on external features (e.g., datacenter ID).
public class UserThreadFactory implements ThreadFactory {
private final String namePrefix;
private final AtomicInteger nextId = new AtomicInteger(1);

UserThreadFactory(String whatFeaturOfGroup) {
namePrefix = "From UserThreadFactory's " + whatFeaturOfGroup + "-Worker-";
}

@Override
public Thread newThread(Runnable task) {
String name = namePrefix + nextId.getAndIncrement();
Thread thread = new Thread(null, task, name, 0, false);
System.out.println(thread.getName());
return thread;
}
}

3. Thread Pool Usage

  • Requirement: Use thread pools instead of creating threads manually.
  • Explanation: Thread pools reduce the overhead of thread creation and destruction, preventing resource exhaustion and excessive context switching.

4. Explicit ThreadPoolExecutor Configuration

  • Requirement: Use ThreadPoolExecutor instead of Executors for creating thread pools.
  • Explanation: Executors can lead to resource exhaustion due to unbounded queues and thread creation.
ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(queueCapacity),
new UserThreadFactory("Datacenter-1")
);

5. Thread-Safe Date Formatting

  • Requirement: Use thread-safe date formatting.
  • Example: Use ThreadLocal for SimpleDateFormat or switch to DateTimeFormatter in JDK 8.
private static final ThreadLocal<DateFormat> df = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

6. ThreadLocal Variable Cleanup

  • Requirement: Clean up ThreadLocal variables, especially in thread pools.
  • Example: Use try-finally blocks to ensure cleanup.
objectThreadLocal.set(userInfo);
try {
// ...
} finally {
objectThreadLocal.remove();
}

7. Locking Performance Considerations

  • Requirement: Minimize the performance impact of locks.
  • Explanation: Prefer lock-free data structures, lock only necessary code blocks, and use object-level locks instead of class-level locks.

8. Consistent Locking Order

  • Requirement: Maintain consistent locking order to prevent deadlocks.
  • Explanation: Ensure that all threads acquire locks in the same order to avoid circular wait conditions.

9. Exception Handling in Locking

  • Requirement: Handle exceptions properly to avoid lock leaks.
  • Example: Ensure that locks are released even if exceptions occur.
Lock lock = new ReentrantLock();
boolean isLocked = lock.tryLock();
if (isLocked) {
try {
doSomething();
doOthers();
} finally {
lock.unlock();
}
}

10. Optimistic and Pessimistic Locking

  • Requirement: Use appropriate locking strategies based on conflict probability.
  • Explanation: Use optimistic locking for low conflict scenarios and pessimistic locking for high conflict scenarios.

11. ScheduledExecutorService for Timed Tasks

  • Requirement: Use ScheduledExecutorService instead of Timer for concurrent timed tasks.
  • Explanation: Timer can terminate all tasks if one throws an uncaught exception.

12. CountDownLatch for Synchronization

  • Requirement: Use CountDownLatch for synchronization in multi-threaded tasks.
  • Explanation: Ensure that all threads call countDown and handle exceptions to avoid blocking the main thread.

13. Random Instance Management

  • Requirement: Avoid sharing Random instances across threads.
  • Example: Use ThreadLocalRandom in JDK 7+ or ensure each thread has its own Random instance.

14. Double-Checked Locking

  • Requirement: Use volatile for double-checked locking to avoid initialization issues.
  • Example: Declare the target variable as volatile.
private volatile Helper helper = null;

15. Volatile and Atomic Operations

  • Requirement: Use volatile for one-write-many-read scenarios and Atomic classes for multi-write scenarios.
  • Example: Use AtomicInteger or LongAdder for atomic operations.

16. HashMap Resize Risks

  • Requirement: Be cautious of HashMap resizing in high-concurrency scenarios.
  • Explanation: Resizing can lead to deadlocks and high CPU usage.

17. ThreadLocal Usage

  • Requirement: Use ThreadLocal with static variables carefully.
  • Explanation: ThreadLocal variables are shared within a thread but can cause issues if not managed properly.

By following these best practices, you can ensure that your Java application is thread-safe, scalable, and robust. These guidelines cover a wide range of scenarios, from basic thread safety to advanced concurrency patterns, helping you build high-performance applications.

Best Practices of Java Core Multi-Threading Programming

· 阅读需 9 分钟

Introduction​

Java's multi-threading capabilities are powerful, but they require careful handling to avoid common pitfalls such as race conditions, deadlocks, and excessive synchronization. This article provides a deep dive into best practices for Java core multi-threading programming, including detailed textual diagrams, explanations, and real-life production sample code.


Thread Safety​

Use Synchronization Wisely​

Synchronization ensures that only one thread can execute a synchronized block or method at a time. However, excessive synchronization can lead to performance bottlenecks.

+-------------------+ +-------------------+
| | | |
| Thread 1 |<----->| Shared Resource|
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Thread 2 |<----->| Shared Resource|
| | | |
+-------------------+ +-------------------+
class Counter {
private int count = 0;

public synchronized void increment() {
count++;
}

public synchronized int getCount() {
return count;
}
}

public class Main {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();

Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

t1.start();
t2.start();

t1.join();
t2.join();

System.out.println("Count: " + counter.getCount()); // Count: 2000
}
}

Avoid Excessive Synchronization​

Excessive synchronization can lead to performance issues. Use finer-grained locks or non-blocking algorithms where possible.

class FineGrainedCounter {
private int count = 0;
private final Object lock = new Object();

public void increment() {
synchronized (lock) {
count++;
}
}

public int getCount() {
synchronized (lock) {
return count;
}
}
}

Use Atomic Classes​

Atomic classes provide thread-safe operations on single variables without the need for explicit synchronization.

import java.util.concurrent.atomic.AtomicInteger;

class AtomicCounter {
private AtomicInteger count = new AtomicInteger(0);

public void increment() {
count.incrementAndGet();
}

public int getCount() {
return count.get();
}
}

public class Main {
public static void main(String[] args) throws InterruptedException {
AtomicCounter counter = new AtomicCounter();

Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
counter.increment();
}
});

t1.start();
t2.start();

t1.join();
t2.join();

System.out.println("Count: " + counter.getCount()); // Count: 2000
}
}

Thread Communication​

Use wait() and notify()​

Threads can communicate using wait(), notify(), and notifyAll() methods.

+-------------------+ +-------------------+
| | | |
| Producer |<----->| Consumer |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Shared Buffer |<----->| Shared Buffer |
| | | |
+-------------------+ +-------------------+
class SharedBuffer {
private int[] buffer = new int[10];
private int count = 0;

public synchronized void produce(int value) throws InterruptedException {
while (count == buffer.length) {
wait();
}
buffer[count++] = value;
notifyAll();
}

public synchronized int consume() throws InterruptedException {
while (count == 0) {
wait();
}
int value = buffer[--count];
notifyAll();
return value;
}
}

class Producer implements Runnable {
private SharedBuffer buffer;

public Producer(SharedBuffer buffer) {
this.buffer = buffer;
}

@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
buffer.produce(i);
System.out.println("Produced: " + i);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class Consumer implements Runnable {
private SharedBuffer buffer;

public Consumer(SharedBuffer buffer) {
this.buffer = buffer;
}

@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
int value = buffer.consume();
System.out.println("Consumed: " + value);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public class Main {
public static void main(String[] args) {
SharedBuffer buffer = new SharedBuffer();
new Thread(new Producer(buffer)).start();
new Thread(new Consumer(buffer)).start();
}
}

Avoid Busy Waiting​

Busy waiting is inefficient and should be avoided. Use wait() and notify() instead.

class BusyWaitExample {
private boolean flag = false;

public synchronized void setFlag(boolean flag) {
this.flag = flag;
notifyAll();
}

public synchronized void waitForFlag() throws InterruptedException {
while (!flag) {
wait();
}
}
}

Thread Pooling​

Use ExecutorService​

Thread pooling manages a pool of worker threads to execute tasks concurrently.

+-------------------+ +-------------------+
| | | |
| Task 1 |<----->| Thread Pool |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Task 2 |<----->| Worker Threads |
| | | |
+-------------------+ +-------------------+
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);

for (int i = 0; i < 10; i++) {
executor.submit(() -> {
System.out.println("Thread " + Thread.currentThread().getName() + " is running");
});
}

executor.shutdown();
}
}

Custom Thread Pools​

Custom thread pools can be created using ThreadPoolExecutor.

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);

for (int i = 0; i < 10; i++) {
executor.submit(() -> {
System.out.println("Thread " + Thread.currentThread().getName() + " is running");
});
}

executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Deadlock Prevention​

Lock Ordering​

Ensure that locks are acquired in a consistent order to prevent deadlocks.

+-------------------+ +-------------------+
| | | |
| Thread 1 |<----->| Resource A |
| | | |
+-------------------+ +-------------------+
| |
| |
v v
+-------------------+ +-------------------+
| | | |
| Thread 2 |<----->| Resource B |
| | | |
+-------------------+ +-------------------+
public class Main {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();

Thread t1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (lock2) {
System.out.println("Thread 1: Holding lock 1 & 2...");
}
}
});

Thread t2 = new Thread(() -> {
synchronized (lock1) { // Acquire locks in the same order
System.out.println("Thread 2: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 2...");
synchronized (lock2) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
});

t1.start();
t2.start();
}
}

Timeout Mechanisms​

Use timeouts to avoid waiting indefinitely for a lock.

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
public static void main(String[] args) {
Lock lock1 = new ReentrantLock();
Lock lock2 = new ReentrantLock();

Thread t1 = new Thread(() -> {
try {
if (lock1.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
if (lock2.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 1: Holding lock 1 & 2...");
lock2.unlock();
}
lock1.unlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});

Thread t2 = new Thread(() -> {
try {
if (lock1.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 2: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
if (lock2.tryLock(1, TimeUnit.SECONDS)) {
System.out.println("Thread 2: Holding lock 1 & 2...");
lock2.unlock();
}
lock1.unlock();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});

t1.start();
t2.start();
}
}

Concurrency Utilities​

Use Concurrent Collections​

Concurrent collections provide thread-safe data structures.

import java.util.concurrent.ConcurrentHashMap;

public class Main {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();

Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
map.put("Key" + i, i);
}
});

Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
map.put("Key" + i, i);
}
});

t1.start();
t2.start();

try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Map size: " + map.size()); // Map size: 1000
}
}

Fork/Join Framework​

The Fork/Join framework is designed for tasks that can be recursively split into smaller tasks.

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

class SumTask extends RecursiveTask<Integer> {
private static final int THRESHOLD = 10;
private int[] array;
private int start;
private int end;

public SumTask(int[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}

@Override
protected Integer compute() {
if (end - start <= THRESHOLD) {
int sum = 0;
for (int i = start; i < end; i++) {
sum += array[i];
}
return sum;
} else {
int mid = (start + end) / 2;
SumTask leftTask = new SumTask(array, start, mid);
SumTask rightTask = new SumTask(array, mid, end);
leftTask.fork();
int rightResult = rightTask.compute();
int leftResult = leftTask.join();
return leftResult + rightResult;
}
}
}

public class Main {
public static void main(String[] args) {
int[] array = new int[100];
for (int i = 0; i < array.length; i++) {
array[i] = i + 1;
}

ForkJoinPool pool = new ForkJoinPool();
int sum = pool.invoke(new SumTask(array, 0, array.length));
System.out.println("Sum: " + sum); // Sum: 5050
}
}

Thread Local Variables​

ThreadLocal variables allow each thread to have its own copy of a variable.

public class Main {
private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

public static void main(String[] args) {
Thread t1 = new Thread(() -> {
threadLocal.set(1);
System.out.println("Thread 1: " + threadLocal.get());
});

Thread t2 = new Thread(() -> {
threadLocal.set(2);
System.out.println("Thread 2: " + threadLocal.get());
});

t1.start();
t2.start();
}
}

Example: Concurrent Web Server​

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class RequestHandler implements Runnable {
private Socket clientSocket;

public RequestHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}

@Override
public void run() {
try {
// Handle the client request
System.out.println("Handling request from " + clientSocket.getInetAddress());
// Simulate processing time
Thread.sleep(1000);
clientSocket.getOutputStream().write("HTTP/1.1 200 OK\r\n\r\nHello, World!".getBytes());
clientSocket.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

public class WebServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
ExecutorService executor = Executors.newFixedThreadPool(10);

while (true) {
Socket clientSocket = serverSocket.accept();
executor.submit(new RequestHandler(clientSocket));
}
}
}

Conclusion​

Java's multi-threading capabilities are powerful, but they require careful handling to avoid common pitfalls such as race conditions, deadlocks, and excessive synchronization. By following the best practices outlined in this article, you can write robust, high-performance Java applications that leverage the full potential of multi-threading. The provided real-life production sample code illustrates how these concepts can be applied in practical scenarios.


References​

  1. Java Concurrency in Practice
  2. Oracle Java Documentation
  3. Java Memory Model
  4. Fork/Join Framework
  5. Concurrent Collections

By mastering these concepts and practices, you can build robust, high-performance Java applications that leverage the full potential of multi-threading.

Best Practices of Java Core ThreadPoolExecutor

· 阅读需 8 分钟

1. Why Executors is Discouraged​

Java’s Executors class provides several factory methods to create various types of thread pools:

public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newFixedThreadPool(int nThreads)
public static ExecutorService newCachedThreadPool()
public static ScheduledExecutorService newSingleThreadScheduledExecutor()
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

These convenience methods simplify thread pool creation, but they do not allow fine-tuning. Using Executors to create thread pools is generally discouraged due to potential risks, such as resource exhaustion. The official guidance is to use ThreadPoolExecutor directly, enabling full control over pool configurations, ensuring stability under varying load conditions.

public class ThreadDemo {
public static void main(String[] args) {
ExecutorService es = Executors.newSingleThreadExecutor();
}
}

Why it's discouraged: Creating a thread pool this way can lead to issues like uncontrolled resource usage. Best practice: use ThreadPoolExecutor instead.

2. ThreadPoolExecutor Introduction​

Java's ThreadPoolExecutor is a cornerstone of concurrent programming. It provides a high-performance, versatile API for managing threads and handling tasks, but it requires careful configuration to prevent resource wastage, improve system responsiveness, and enhance maintainability.


3. Overview of ThreadPoolExecutor​

The ThreadPoolExecutor manages thread allocation, scaling, and task queueing while optimizing thread lifecycle to minimize system overhead. Here is a basic textual model of a ThreadPoolExecutor lifecycle and memory model:

+-----------------------------+
| Task Queue |
| (BlockingQueue<Runnable>) |
+------------+----------------+
|
+-----------------v-----------------+
| |
| ThreadPoolExecutor |
| |
+----------------+------------------+
|
+----------------------+--------------------+
| Worker Threads |
| +-----------------------------------+ |
| | Worker Thread 1 | |
| |-----------------------------------| |
| | Worker Thread 2 | |
| |-----------------------------------| |
| | Worker Thread N | |
+-------------------------------------------+
  • Worker Threads: Execute tasks within a controlled environment.
  • Task Queue: Holds tasks awaiting execution, preventing unnecessary thread creation.
  • ThreadPoolExecutor: Controls the execution strategy, pool scaling, and task rejection policies.

To avoid risks, use ThreadPoolExecutor directly for creating thread pools:

public class ThreadDemo {
public static void main(String[] args) {
ExecutorService es = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(10), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardPolicy());
}
}

5. Core Configuration Parameters​

The ThreadPoolExecutor relies on several key configuration parameters:

  • corePoolSize: Minimum number of threads in the pool.
  • maximumPoolSize: Maximum threads allowed in the pool.
  • keepAliveTime: The time a thread can remain idle before termination if beyond corePoolSize.
  • workQueue: Queue holding tasks before they are assigned to threads.

Best Practice: Choose configuration values based on workload type and system capacity.​


6. Analyzing Executors Source Code​

The methods newSingleThreadExecutor(), newFixedThreadPool(int nThreads), and newCachedThreadPool() all internally use ThreadPoolExecutor. Below are the source code implementations from JDK 8:

public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(
new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())
);
}

public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}

public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}

All three methods use ThreadPoolExecutor but provide different default configurations. These static methods abstract parameters, which can lead to undesired behaviors under high loads.

7. ThreadPoolExecutor Constructors and Parameters​

When creating thread pools with ThreadPoolExecutor, initializing parameters must be specified. One of its constructors takes the following parameters:

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)

Parameter Breakdown​

  • corePoolSize: Number of threads to keep in the pool.
  • maximumPoolSize: Maximum number of threads allowed.
  • keepAliveTime: Time limit for idle threads over corePoolSize before termination.
  • unit: Unit of time for keepAliveTime.
  • workQueue: Queue to hold tasks before execution.
  • threadFactory: Factory for creating threads, usually Executors.defaultThreadFactory().
  • handler: Policy for handling tasks when the pool reaches its limit.

8. Advanced Queue Management​

Choosing the Right Queue:

  1. SynchronousQueue: No internal capacity, ideal for transient spikes.
  2. LinkedBlockingQueue: Unbounded, suitable for long-running processes.
  3. ArrayBlockingQueue: Fixed size, optimal for bounded workloads.

Note: Select a queue type aligned with task load, duration, and resource availability.

Relationships between corePoolSize and maximumPoolSize​

  • If thread count < corePoolSize, a new thread is created for each task.
  • If thread count >= corePoolSize, tasks are added to the task queue.
  • If the queue is full and thread count < maximumPoolSize, a new thread is created.
  • If thread count >= maximumPoolSize, the handler policy determines the behavior.

Note: These relationships vary with the type of queue (bounded or unbounded). Unbounded queues will prevent maximum thread creation from being reached.

Understanding workQueue Types​

The workQueue holds pending tasks that await execution. Different queue types handle tasks differently:

  • SynchronousQueue: A direct hand-off mechanism with no capacity. If no thread is immediately available, a new one is created or the task is rejected based on maximumPoolSize.
  • LinkedBlockingQueue: An unbounded queue. When tasks exceed corePoolSize, they are added to the queue instead of creating new threads. Warning: Unbounded growth may exhaust memory if tasks are generated faster than processed.

9. Thread Pool Scaling Strategies​

Efficient scaling balances resource use against workload demands. Consider using dynamic scaling mechanisms for responsive systems.

  • FixedThreadPool: When task load and timing are predictable.
  • CachedThreadPool: For unpredictable or short-lived task loads.

10. RejectedExecutionHandler: Strategies for Handling Excess Tasks​

When the pool and queue are full, the handler parameter specifies a strategy to handle tasks. Java provides four built-in policies:

  • DiscardOldestPolicy: Removes the oldest task in the queue to accommodate the new task.
  • CallerRunsPolicy: Executes the task in the caller thread if the pool is busy, providing a backpressure mechanism.
  • DiscardPolicy: Silently discards the task without further action.
  • AbortPolicy: Throws an exception to signal that the pool is at capacity.

Note: Customize RejectedExecutionHandler based on workload characteristics. Expecially for applications where resilience is critical, consider implementing a custom rejection handler or using CallerRunsPolicy to limit load. Proper configuration can prevent issues like out-of-memory errors.

RejectedExecutionHandler customHandler = new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println("Task rejected: " + r.toString());
// custom logic for rejected task
}
};

11. Enhanced Monitoring Techniques​

Monitoring thread and queue activity is essential for real-time scaling and troubleshooting. Use the following tools and approaches:

  • JMX (Java Management Extensions): For real-time metrics on threads, tasks, and system load.
  • Thread Pool Executor's getPoolSize and getQueue().size() Methods: To track runtime capacity and pending task counts.

12. Backpressure Handling in Production​

Backpressure handling in high-demand environments prevents overloads by controlling the flow of tasks:

  • Rejection Policies: Define fallback strategies for rejected tasks.
  • Queue Sizing: Set boundaries on task input rate.

Diagram: Dynamic Task Flow and Backpressure​

Client Requests
|
+----v----+ Backpressure Control
| Task |--------------------+
| Queue | |
+---------+ |
| |
+------v-------+ |
| Thread | |
| Pool | |
+--------------+----------------+

13. Example​

import java.util.concurrent.*;

public class OptimizedThreadPoolExecutor {
public static void main(String[] args) {
// 1. Configure core parameters with scaling strategy
ThreadPoolExecutor executor = new ThreadPoolExecutor(
4, // core pool size
10, // maximum pool size
30, // keep-alive time
TimeUnit.SECONDS, // keep-alive time unit
new ArrayBlockingQueue<>(100), // task queue type
new ThreadPoolExecutor.CallerRunsPolicy() // rejection policy
);

// 2. Simulate task submission
for (int i = 0; i < 200; i++) {
executor.submit(new Task(i));
}

executor.shutdown();
}

static class Task implements Runnable {
private final int taskId;

Task(int taskId) {
this.taskId = taskId;
}

@Override
public void run() {
System.out.println("Executing Task " + taskId);
try {
Thread.sleep(500); // Simulate work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}

14. Conclusion​

Summary: Use ThreadPoolExecutor directly with appropriately sized pools, tailored queue types, and effective rejection policies to ensure optimal, safe performance. Mastering ThreadPoolExecutor for complex, production-grade applications requires an understanding of core parameters, workload patterns, and monitoring needs. These best practices are intended to improve performance, efficiency, and system resilience in multithreaded environments.