Java并发编程实例--18.修改锁的公平性
ReentrantLock和ReentrantReadWriteLock类的构造函数可接受一个布尔类型参数fair,表示你可以控制这2个类的行为。
其默认值为false,代表non-fair(不公平)模式。
这种模式下,如果有多个线程等待获得锁并且只能有一个线程获得,上面2个锁类会无条件选择一个。(也就是随机抽一个)值为true的话就表示fair模式,即公平模式。
意思是有个先来后到,就跟咱们买东西要排队一样。锁会分给那个等了最长时间的线程。以上逻辑仅适用于lock() and unlock()方法。因为例如tryLock()方法压根不会让线程睡眠。
本例中,我们将修改一下之前使用锁机制同步代码块的程序,加入这个参数来对比一下fair和non-fair两种模式的差别。
PrintQueue.java
package com.dylan.thread.ch2.c06.task;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* This class simulates a print queue.
*
*/
public class PrintQueue {
/**
* Creates a lock to control the access to the queue.
* With the boolean attribute, we control the fairness of
* the Lock
*/
private final Lock queueLock=new ReentrantLock(false);
/**
* Method that prints the Job. The printing is divided in two phase two
* show how the fairness attribute affects the election of the thread who
* has the control of the lock
* @param document The document to print
*/
public void printJob(Object document){
queueLock.lock();
try {
Long duration=(long)(Math.random()*10000);
System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),(duration/1000));
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
queueLock.unlock();
}
queueLock.lock();
try {
Long duration=(long)(Math.random()*10000);
System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),(duration/1000));
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
queueLock.unlock();
}
}
}
Job.java
package com.dylan.thread.ch2.c06.task;
/**
* This class simulates a job that send a document to print
*
*/
public class Job implements Runnable {
/**
* The queue to send the documents
*/
private PrintQueue printQueue;
/**
* Constructor of the class. Initializes the print queue
* @param printQueue the print queue to send the documents
*/
public Job(PrintQueue printQueue){
this.printQueue=printQueue;
}
/**
* Core method of the Job. Sends the document to the queue
*/
@Override
public void run() {
System.out.printf("%s: Going to print a job\n",Thread.currentThread().getName());
printQueue.printJob(new Object());
System.out.printf("%s: The document has been printed\n",Thread.currentThread().getName());
}
}
Main.java
package com.dylan.thread.ch2.c06.core;
import com.dylan.thread.ch2.c06.task.Job;
import com.dylan.thread.ch2.c06.task.PrintQueue;
/**
* Main class of the example
*
*/
public class Main {
/**
* Main method of the example
* @param args
*/
public static void main (String args[]){
// Creates the print queue
PrintQueue printQueue=new PrintQueue();
// Cretes ten jobs and the Threads to run them
Thread thread[]=new Thread[10];
for (int i=0; i<10; i++){
thread[i]=new Thread(new Job(printQueue),"Thread "+i);
}
// Launch a thread ever 0.1 seconds
for (int i=0; i<10; i++){
thread[i].start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行结果:
1.不公平模式:
Thread 0: Going to print a job
Thread 0: PrintQueue: Printing a Job during 0 seconds
Thread 1: Going to print a job
Thread 2: Going to print a job
Thread 3: Going to print a job
Thread 4: Going to print a job
Thread 5: Going to print a job
Thread 6: Going to print a job
Thread 0: PrintQueue: Printing a Job during 1 seconds
Thread 7: Going to print a job
Thread 8: Going to print a job
Thread 9: Going to print a job
Thread 1: PrintQueue: Printing a Job during 1 seconds
Thread 0: The document has been printed
Thread 1: PrintQueue: Printing a Job during 8 seconds
Thread 1: The document has been printed
Thread 2: PrintQueue: Printing a Job during 3 seconds
Thread 2: PrintQueue: Printing a Job during 8 seconds
Thread 2: The document has been printed
Thread 3: PrintQueue: Printing a Job during 6 seconds
Thread 3: PrintQueue: Printing a Job during 5 seconds
Thread 3: The document has been printed
Thread 4: PrintQueue: Printing a Job during 9 seconds
Thread 5: PrintQueue: Printing a Job during 3 seconds
Thread 5: PrintQueue: Printing a Job during 1 seconds
Thread 6: PrintQueue: Printing a Job during 1 seconds
Thread 5: The document has been printed
Thread 6: PrintQueue: Printing a Job during 6 seconds
Thread 6: The document has been printed
Thread 7: PrintQueue: Printing a Job during 8 seconds
Thread 7: PrintQueue: Printing a Job during 5 seconds
Thread 7: The document has been printed
Thread 8: PrintQueue: Printing a Job during 7 seconds
Thread 8: PrintQueue: Printing a Job during 5 seconds
Thread 8: The document has been printed
Thread 9: PrintQueue: Printing a Job during 1 seconds
Thread 9: PrintQueue: Printing a Job during 3 seconds
Thread 9: The document has been printed
Thread 4: PrintQueue: Printing a Job during 7 seconds
Thread 4: The document has been printed
2.公平模式:
Thread 0: Going to print a job
Thread 0: PrintQueue: Printing a Job during 1 seconds
Thread 1: Going to print a job
Thread 2: Going to print a job
Thread 3: Going to print a job
Thread 4: Going to print a job
Thread 5: Going to print a job
Thread 6: Going to print a job
Thread 7: Going to print a job
Thread 8: Going to print a job
Thread 9: Going to print a job
Thread 1: PrintQueue: Printing a Job during 8 seconds
Thread 2: PrintQueue: Printing a Job during 9 seconds
Thread 3: PrintQueue: Printing a Job during 0 seconds
Thread 4: PrintQueue: Printing a Job during 3 seconds
Thread 5: PrintQueue: Printing a Job during 1 seconds
Thread 6: PrintQueue: Printing a Job during 0 seconds
Thread 7: PrintQueue: Printing a Job during 7 seconds
Thread 8: PrintQueue: Printing a Job during 3 seconds
Thread 9: PrintQueue: Printing a Job during 4 seconds
Thread 0: PrintQueue: Printing a Job during 6 seconds
Thread 0: The document has been printed
Thread 1: PrintQueue: Printing a Job during 9 seconds
Thread 1: The document has been printed
Thread 2: PrintQueue: Printing a Job during 4 seconds
Thread 2: The document has been printed
Thread 3: PrintQueue: Printing a Job during 3 seconds
Thread 3: The document has been printed
Thread 4: PrintQueue: Printing a Job during 1 seconds
Thread 5: PrintQueue: Printing a Job during 3 seconds
Thread 4: The document has been printed
Thread 5: The document has been printed
Thread 6: PrintQueue: Printing a Job during 0 seconds
Thread 6: The document has been printed
Thread 7: PrintQueue: Printing a Job during 0 seconds
Thread 7: The document has been printed
Thread 8: PrintQueue: Printing a Job during 3 seconds
Thread 9: PrintQueue: Printing a Job during 1 seconds
Thread 8: The document has been printed
Thread 9: The document has been printed
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构