Home >Java >javaTutorial >How to implement concurrent programming using semaphores in Java?

How to implement concurrent programming using semaphores in Java?

王林
王林Original
2024-05-01 09:33:021187browse

By using semaphores, concurrent programming in Java can control access to shared resources. Specific steps include: create a semaphore, obtain the semaphore (continue if available, otherwise block), and release the semaphore. In a practical case, a semaphore is used to control access to the printer, allowing only one thread to print at a time.

如何使用 Java 中的信号量实现并发编程?

How to use semaphores in Java to implement concurrent programming

In Java, semaphores are a concurrent programming primitive , which allows us to control access to shared resources. Semaphores are implemented by maintaining a counter that represents the available amount of a resource. When a thread needs to access a resource, it will try to acquire the semaphore. If the semaphore is available, the thread can continue execution. Otherwise, the thread will be blocked until the semaphore is available.

Implementing the semaphore

The Semaphore class in Java provides an implementation of the semaphore. We can create a semaphore using the following steps:

import java.util.concurrent.Semaphore;

public class MySemaphore {

    private Semaphore semaphore;

    public MySemaphore(int permits) {
        semaphore = new Semaphore(permits);
    }

    public void acquire() throws InterruptedException {
        semaphore.acquire();
    }

    public void release() {
        semaphore.release();
    }
}

Practical Case

Consider the following scenario: We have a shared printer and multiple threads need to use it . To prevent threads from accessing the printer at the same time, we will use a semaphore to control access to the printer.

import java.util.concurrent.Semaphore;

public class Printer {

    private Semaphore semaphore = new Semaphore(1);

    public void print(String document) {
        try {
            semaphore.acquire();
            // 打印文档
        } finally {
            semaphore.release();
        }
    }
}

public class Main {

    public static void main(String[] args) {
        Printer printer = new Printer();

        Thread thread1 = new Thread(() -> printer.print("Document 1"));
        Thread thread2 = new Thread(() -> printer.print("Document 2"));

        thread1.start();
        thread2.start();
    }
}

In this example, we create a Printer class that contains a Semaphore instance. The print method uses the acquire method to acquire the semaphore, allowing the thread to continue executing only when the semaphore is available. Finally, the release method releases the semaphore after finishing printing so that other threads can use the printer.

The above is the detailed content of How to implement concurrent programming using semaphores in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn