Intranet Penetration|Imitation of Shenzhuo Internet's Delayed Bandwidth Technology Implementation

Guava is an extension project of a collection of java class libraries developed by Google. Based on the widely used java1.6, these high-quality APIs provide powerful extension functions and make your java code more elegant and concise. This article uses the RateLimiter class under the Guava concurrent package to implement the flow control function.

Before introducing RateLimiter, let's take a look at the Semaphore class that comes with java.

Semaphore is located under java.Util.concurrent and can be used to control concurrency. When creating a Semaphore object, you can specify the number of concurrency. When the thread starts, the acquire method is called, and the current available concurrency number is reduced by 1. When the thread concurrency reaches the specified concurrency number, That is, when the available concurrency is 0, the thread waits.

The following is a simple example of using Semaphore to implement concurrency control:

import java.util.concurrent.Semaphore;

public class SemaphoreTest {
    public static void main(String[] args) {
        Test t = new Test();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                t.print();
            }).start();
        }
    }
}

class Test {
    private static final Semaphore sp = new Semaphore(3);

    public void print() {
        try {
            sp.acquire();
            System.out.println(Thread.currentThread().getName()+" started: "+sp.availablePermits());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            sp.release();
            System.out.println(Thread.currentThread().getName()+" released: "+sp.availablePermits());
        }
    }
}

After understanding how to use Semaphore, let’s look at the use of RateLimiter. In fact, the usage of the two is very similar, but it is worth noting that Semaphore limits the number of concurrency, while RateLimiter limits the number of concurrency per unit time. That is, the concurrent rate.

The following is a simple example of using RateLimiter to implement concurrency control. The number of concurrency per second is set to 1. Readers can compare the above Semaphore code to feel the difference.

import com.google.common.util.concurrent.RateLimiter;

public class RateLimiterTest {
    public static void main(String[] args){
        TestMethods tm = new TestMethods();
        for(int i=0;i<5;i++){
            new Thread(()->{
                tm.print();
            }).start();
        }
    }
}

class TestMethods{
    public void print(){
        try {
            Constants.rl.acquire();
            System.out.println("Thread: "+Thread.currentThread().getName()+"RateLimiter: "+Constants.rl.getRate());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Constants{
    public static final RateLimiter rl = RateLimiter.create(1.0);
}

Guess you like

Origin blog.csdn.net/u010905359/article/details/119919029