Java Concurrency Learning 16--Thread Sync tool semaphore (semaphores)

Source: Internet
Author: User

This article is a summary of the study of the article on the network, thank you for your selfless sharing.

When a thread wants to access a shared resource, first, it must get semaphore. If the value of the internal counter of semaphore is greater than 0, then semaphore reduces the value of the counter and allows access to the shared resource. A counter with a value greater than 0 indicates that there are resources that are free to use, so threads can access and use them.

Package Chapter3;import Java.util.concurrent.semaphore;public class PrintQueue2 {private final Semaphore Semaphore; Public PrintQueue2 () {semaphore = new semaphore (1);} public void PrintJob (Object document) {try {semaphore.acquire (); Long duration = (long) (Math.random () *10); System.out.println (Thread.CurrentThread (). GetName () + "  PrintQueue   " +duration);} catch ( Interruptedexception e) {e.printstacktrace ();} Finally{semaphore.release ();}}}

Package Chapter3;public class Job implements Runnable{private PrintQueue2 printqueue;public Job (PrintQueue2 printQueue) {this.printqueue = PrintQueue;} @Overridepublic void Run () {System.out.printf ("%s:going to print a job\n", Thread.CurrentThread (). GetName ()); Printqueue.printjob (New Object ()); System.out.printf ("%s:the document has been printed\n", Thread.CurrentThread (). GetName ());}}

Package Chapter3;public class Main {/** * <p> * </p> * @author Zhangjunshuai * @date 2014-9-23 pm 8:45:31 * @pa Ram args */public static void main (string[] args) {PrintQueue2 printQueue = new PrintQueue2 (); Thread thread[] = new thread[10];for (int i=0;i<10;i++) {thread[i] = new Thread (new Job (printQueue), "thread" +i);} for (int i=0;i<10;i++) {Thread[i].start ();}}}

Can modify the fairness of the semaphores, in the default case the entry of the semaphore is not fair. If the second parameter of the initialization is set to true, the time-to-wait entry is selected.

Reference:

Concurrent Network

Lao Zi's Concurrent study

Java Concurrency Learning 16--Thread Sync tool semaphore (semaphores)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.