This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August
Semaphore that controls the number of concurrent threads
Semaphore is used to control the number of threads accessing a particular resource at the same time. It coordinates threads to ensure proper use of common resources.
public class SemaphoreTest {
private static final int THREAD_COUNT = 30;
private static ExecutorService threadPool = Executors
.newFixedThreadPool(THREAD_COUNT);
private static Semaphore s = new Semaphore(10);
public static void main(String[] args) {
for (int i = 0; i < THREAD_COUNT; i++) {
threadPool.execute(new Runnable() {
@Override
public void run(a) {
try {
s.acquire();
System.out.println("save data");
s.release();
} catch(InterruptedException e) { } } }); } threadPool.shutdown(); }}Copy the code
Semaphore is also very simple to use. First the thread uses Semaphore’s acquire() method to acquire a license, and then calls Release () to return the license. You can also try to obtain a license using the tryAcquire() method.
Data exchange between threads
Sanoer is a tool class used for collaboration between threads. Used for data exchange between threads. It provides a synchronization point at which two threads can exchange data with each other. The two threads exchange data using the Exchange method. If the first thread executes the Exchange () method first, it will wait until the second thread executes the exchange method. When both threads reach the synchronization point, the two threads can exchange data, passing the data produced by each thread to the other.
If either thread does not execute the exchange() method, it will wait forever. To avoid waiting, you can use exchange(V x, longTimeout, TimeUnit Unit) to set the maximum waiting time.
Sano1100it is used in the genetic algorithm, which needs to select two people for mating, and the data of the two people is exchanged and the crossover rule is used to obtain two mating results. For example, we need to manually input the paper bank statement into electronic bank statement. In order to avoid mistakes, two people from AB are used for input. After input into Excel, the system needs to load the two Excel and proofread the two Excel data to check whether the input is consistent
public class ExchangerTest {
private static final Exchanger<String> exgr = new Exchanger<String>();
private static ExecutorService threadPool = Executors.newFixedThreadPool(2);
public static void main(String[] args) {
threadPool.execute(new Runnable() {
@Override
public void run(a) {
try {
String A = "Bank statement A"; // A Input bank statement data
exgr.exchange(A);
} catch (InterruptedException e) {
}
}
});
threadPool.execute(new Runnable() {
@Override
public void run(a) {
try {
String B = "Bank statement B"; // B input bank statement data
String A = exgr.exchange("B");
System.out.println("Are data A and B consistent?" + A.equals(B) + ", A typed:"+ A + ", B input is: + B);
} catch(InterruptedException e) { } } }); threadPool.shutdown(); }}Copy the code