分布式并发设计模式详解
在分布式开发中,除了常规的23种设计模式外,还有一些针对并发场景的常用设计模式,本文将对这些模式进行详细介绍。
1. 单例模式(singleton)
原理:
并发代码示例:
public class singleton { private static volatile singleton instance; private singleton() { // 私有构造函数 } public static singleton getinstance() { if (instance == null) { // 第一次检查 synchronized (singleton.class) { // 同步锁 if (instance == null) { // 第二次检查 instance = new singleton(); } } } return instance; } }
2. 不可变对象模式(immutable object)
原理:
并发代码示例:
public final class immutableperson { private final string name; private final int age; public immutableperson(string name, int age) { this.name = name; this.age = age; } public string getname() { return name; } public int getage() { return age; } public immutableperson setname(string newname) { return new immutableperson(newname, age); } public immutableperson setage(int newage) { return new immutableperson(name, newage); } }
3. 线程局部存储模式(thread local storage)
原理:
并发代码示例:
public class threadlocalexample { // 定义一个threadlocal变量,用于存储线程级别的变量 private static final threadlocal<string> threadlocal = new threadlocal<string>(); public static void setthreadlocalvalue(string value) { threadlocal.set(value); } public static string getthreadlocalvalue() { return threadlocal.get(); } public static void main(string[] args) { // 在主线程中设置和获取threadlocal变量的值 setthreadlocalvalue("main thread value"); system.out.println("main thread value: " + getthreadlocalvalue()); // 创建一个新线程并设置和获取threadlocal变量的值 thread thread = new thread(() -> { setthreadlocalvalue("child thread value"); system.out.println("child thread value: " + getthreadlocalvalue()); }); thread.start(); } }
4. 生产者-消费者模式(producer-consumer)
原理:
并发代码示例:
import java.util.concurrent.blockingqueue; import java.util.concurrent.linkedblockingqueue; class producer implements runnable { private final blockingqueue<integer> queue; public producer(blockingqueue<integer> q) { queue = q; } public void run() { try { for (int i = 0; i < 10; i++) { queue.put(i); system.out.println("produced: " + i); } } catch (interruptedexception ex) { ex.printstacktrace(); } } } class consumer implements runnable { private final blockingqueue<integer> queue; public consumer(blockingqueue<integer> q) { queue = q; } public void run() { try { while (true) { int value = queue.take(); system.out.println("consumed: " + value); } } catch (interruptedexception ex) { ex.printstacktrace(); } } } public class producerconsumerexample { public static void main(string[] args) { blockingqueue<integer> queue = new linkedblockingqueue<integer>(10); producer producer = new producer(queue); consumer consumer = new consumer(queue); new thread(producer).start(); new thread(consumer).start(); } }
5. 读者-写者模式(read-write lock)
原理:
并发代码示例:
import java.util.concurrent.locks.readwritelock; import java.util.concurrent.locks.reentrantreadwritelock; class sharedresource { private final readwritelock readwritelock = new reentrantreadwritelock(); private final lock readlock = readwritelock.readlock(); private final lock writelock = readwritelock.writelock(); private string data; public void read() { readlock.lock(); try { // 读取数据 system.out.println("reading data: " + data); } finally { readlock.unlock(); } } public void write(string newdata) { writelock.lock(); try { // 写入数据 data = newdata; system.out.println("writing data: " + data); } finally { writelock.unlock(); } } } public class readwritelockexample { public static void main(string[] args) { sharedresource resource = new sharedresource(); // 创建多个读者线程 for (int i = 0; i < 5; i++) { new thread(resource::read).start(); } // 创建写者线程 new thread(() -> resource.write("new data")).start(); } }
6. 工作队列模式(worker thread)
原理:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Task implements Runnable { private final int taskId; public Task(int taskId) { this.taskId = taskId; } @Override public void run() {
以上就是分布式并发设计模式有哪些及其实现原理?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号