并发编程
大约 2 分钟
Java 并发编程入门
Java 作为一种广泛使用的编程语言,在开发高性能和可伸缩的应用程序时,并发编程是一个重要的技术点。本文将介绍 Java 并发编程的基础知识,包括线程的创建、线程池的使用以及常见的并发工具类。
1. 线程的创建
在 Java 中,可以通过两种方式创建线程:
1.1 继承 Thread
类
通过继承 Thread
类并重写 run
方法来创建线程。以下是一个示例:
class MyThread extends Thread {
public void run() {
System.out.println("MyThread is running.");
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
1.2 实现 Runnable
接口
通过实现 Runnable
接口并将其实例传递给 Thread
对象来创建线程。以下是一个示例:
class MyRunnable implements Runnable {
public void run() {
System.out.println("MyRunnable is running.");
}
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}
2. 线程池
线程池用于管理和复用线程资源,避免频繁创建和销毁线程带来的性能开销。Java 提供了 java.util.concurrent
包中的 ExecutorService
接口来实现线程池。
2.1 使用 Executors
创建线程池
以下是使用 Executors
创建固定大小线程池的示例:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
executorService.execute(new RunnableTask());
}
executorService.shutdown();
}
}
class RunnableTask implements Runnable {
public void run() {
System.out.println("Task is running in: " + Thread.currentThread().getName());
}
}
3. 常见并发工具类
Java 提供了一些常见的并发工具类来简化并发编程。
3.1 CountDownLatch
CountDownLatch
用于让一个或多个线程等待其他线程完成某些操作。以下是一个示例:
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(3);
for (int i = 0; i < 3; i++) {
new Thread(new Worker(latch)).start();
}
latch.await();
System.out.println("All workers are done.");
}
}
class Worker implements Runnable {
private final CountDownLatch latch;
Worker(CountDownLatch latch) {
this.latch = latch;
}
public void run() {
System.out.println(Thread.currentThread().getName() + " is working.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
latch.countDown();
}
}
}
3.2 CyclicBarrier
CyclicBarrier
用于让一组线程互相等待,直到到达一个共同的屏障点。以下是一个示例:
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierExample {
public static void main(String[] args) {
int parties = 3;
CyclicBarrier barrier = new CyclicBarrier(parties, new BarrierAction());
for (int i = 0; i < parties; i++) {
new Thread(new Worker(barrier)).start();
}
}
}
class Worker implements Runnable {
private final CyclicBarrier barrier;
Worker(CyclicBarrier barrier) {
this.barrier = barrier;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " is waiting at the barrier.");
barrier.await();
System.out.println(Thread.currentThread().getName() + " has crossed the barrier.");
} catch (InterruptedException | BrokenBarrierException e) {
Thread.currentThread().interrupt();
}
}
}
class BarrierAction implements Runnable {
public void run() {
System.out.println("All parties have reached the barrier. Performing the barrier action.");
}
}
结论
Java 并发编程是构建高效、可伸缩应用程序的关键。通过理解线程的创建方式、线程池的使用以及常见的并发工具类,可以更好地管理并发任务,提高程序的性能和可靠性。希望本文对你理解 Java 并发编程有所帮助。