并发编程:线程同步基础:4、公平锁

目录

主要方法

1、主程序

2、打印队列

3、打印任务

4、testFair()的执行结果

5、testNoFair()的执行结果


主要方法

new ReentrantLock(fair);

reentrantlock提供一个参数,当参数为true时,锁为公平锁(即最长等待优先策略)。当参数为false时采用随机策略(默认为false)

下面设计2中方式的任务,看看程序输出结果

1、主程序

package xyz.jangle.thread.test.n2_4.lock;

/**
 * 
 * 公平锁(最长等待优先策略)
 * 重入锁的公平参数的使用
 * @author jangle
 * @email [email protected]
 * @time 2020年7月21日 下午10:10:45
 * 
 */
public class M {

	public static void main(String[] args) {

		testFair();

//		testNoFair();

	}

	/**
	 * 	采用随机,没有优先的策略
	 * 
	 * @author jangle
	 * @time 2020年7月21日 下午10:19:18
	 */
	public static void testNoFair() {
		PrintQueue queue = new PrintQueue(false);
		for (int j = 0; j < 10; j++) {
			new Thread(new Job(queue), "Thread" + j).start();
			try {
				Thread.sleep(500L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 	使用公平锁(最长等待优先的策略)
	 * 
	 * @author jangle
	 * @time 2020年7月21日 下午10:18:55
	 */
	public static void testFair() {
		PrintQueue queue = new PrintQueue(true);
		for (int j = 0; j < 10; j++) {
			new Thread(new Job(queue), "Thread" + j).start();
			try {
				Thread.sleep(500L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

2、打印队列

package xyz.jangle.thread.test.n2_4.lock;

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

/**
 * @author jangle
 * @email [email protected]
 * @time 2020年7月21日 下午10:00:23
 * 
 */
public class PrintQueue {

	private Lock queueLock;

	public PrintQueue(boolean fair) {
		super();
		this.queueLock = new ReentrantLock(fair);
	}

	public void printJob(Object document) {
		queueLock.lock();
		Long l = (long) (Math.random() * 1000);
		try {
			Thread.sleep(l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			queueLock.unlock();
		}

	}

}

3、打印任务

package xyz.jangle.thread.test.n2_4.lock;

/**
 * @author jangle
 * @email [email protected]
 * @time 2020年7月21日 下午10:08:09
 * 
 */
public class Job implements Runnable {

	private PrintQueue queue;

	public Job(PrintQueue queue) {
		super();
		this.queue = queue;
	}

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "working");
		queue.printJob(new Object());
		System.out.println(Thread.currentThread().getName() + "worked");

	}

}

4、testFair()的执行结果

Thread0working
Thread1working
Thread0worked
Thread1worked
Thread2working
Thread2worked
Thread3working
Thread3worked
Thread4working
Thread4worked
Thread5working
Thread6working
Thread5worked
Thread6worked
Thread7working
Thread7worked
Thread8working
Thread9working
Thread8worked
Thread9worked

5、testNoFair()的执行结果

Thread0working
Thread1working
Thread0worked
Thread2working
Thread3working
Thread1worked
Thread2worked
Thread4working
Thread3worked
Thread4worked
Thread5working
Thread6working
Thread5worked
Thread6worked
Thread7working
Thread8working
Thread7worked
Thread9working
Thread8worked
Thread9worked

        可见,testFair执行结果更有序。

你可能感兴趣的:(并发编程,#,线程同步基础,java,开发语言)