当前位置:网站首页>Concurrent programming -- countdownlatch combined with thread pool

Concurrent programming -- countdownlatch combined with thread pool

2022-06-13 05:47:00 Coffee is not bitter**

In distributed projects , Realize relevant function calls through distributed architecture , This is inevitable . I'm in the project , For example, the display of the product details information page , Share scenarios such as graph composition , It will involve several sub module functions to obtain information .CountdownLatch It can well realize the function realization of similar scenarios .

CountdownLatch Basic introduction

It can make one thread wait for other threads to finish their work before executing .
details : Concurrent programming –CountdownLatch && CyclicBarrier

Scenario introduction

combination actor and course Information is combined into a sharing diagram , Process only , Core code emulation .

Code implementation

public class CountDownLatchService {
    

	protected Actor actor;
	protected Course course;

	protected CountDownLatchService(Actor actor, Course course) {
    
		this.actor = actor;
		this.course = course;
	}


	private static final int threadSize = 5;
	private boolean isRunning = false;
	private static final CountDownLatch latch = new CountDownLatch(2);
	private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
			threadSize + 1,
			threadSize + 1,
			10,
			TimeUnit.SECONDS
			, new SynchronousQueue<>());


	public int init() {
    
		if (isRunning == true) {
    
			System.out.println(" The thread is running ...");
			return -1;
		}
		isRunning = true;
		executor.execute(() -> {
    
			System.out.println(" obtain actor Related information ");
			course.setName(" Drowsy courseName");
			try {
    
				Thread.sleep(3000);
			} catch (InterruptedException e) {
    
				e.printStackTrace();
			}
			latch.countDown();
			System.out.println("1:"+latch.getCount());
		});
		executor.execute(() -> {
    
			System.out.println(" obtain course Of name");
			actor.setName(" Drowsy actorName");
			try {
    
				Thread.sleep(3000);
			} catch (InterruptedException e) {
    
				e.printStackTrace();
			}
			latch.countDown();
			System.out.println("2:"+latch.getCount());
		});
		executor.execute(() -> {
    
			System.out.println(" obtain course Of type");
			// Just test the simulation , A real project may be a distributed related task invocation 
			course.setType(1L);
			try {
    
				Thread.sleep(3000);
			} catch (InterruptedException e) {
    
				e.printStackTrace();
			}
			latch.countDown();
			System.out.println("3:"+latch.getCount());
		});
		executor.execute(() -> {
    
			System.out.println(" Wait for all information to return =");
			try {
    
				System.out.println("value:"+latch.getCount());
				latch.await();
				// Set up isRunning=false
				isRunning = false;
			} catch (InterruptedException e) {
    
				e.printStackTrace();
			}
			System.out.println("course info= " + course.getName());
			System.out.println("actor info= " + actor.getName());
			// Perform additional splicing synthesis logic 
			System.out.println(" Mosaic picture complete ");
		});

		executor.shutdown();
		System.out.println(" Message return end ");
		return 0;
	}


	public static void main(String[] args) {
    
		Actor actor = new Actor();
		Course course = new Course();
		CountDownLatchService countDownLatchService = new CountDownLatchService(actor, course);
		countDownLatchService.init();
	}
}

Something to watch out for

CountDownLatch Initial values need to be carefully considered , Such as the above demo, I need three threads to get information to execute , Every time you execute latch.countDown(),count It will decrease 1. When count by 0 When , Will execute await.
What do you mean ?
For example, your initial value is 2, Three were executed latch.countDown();
For example, your initial value is 4, Three were executed latch.countDown();
The two results must be different , If you look at the source code, you will find , All calls await The thread of the method will be blocked in AQS In the blocking queue , By judgment count Is it 0 To decide whether to evoke .

原网站

版权声明
本文为[Coffee is not bitter**]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507384488.html