Java Multithreaded Learning notes--use of semaphores

Source: Internet
Author: User
Tags semaphore

In Java, when you control multi-threaded access to resources, you use semaphores to control multiple threads accessing a resource at the same time.

There are two methods of construction:

public Semaphore (int permits)

Public Semaphore (int Permits,boolean Fair)

The second parameter, like a re-entry lock, is a fair designation. (Fairness is about sacrificing performance.)

1  Public classSignalnumImplementsRunnable {2Semaphore semaphore=NewSemaphore (2);3 4 5 @Override6      Public voidrun () {7         Try {8 Semaphore.acquire ();9              for(inti=1;i<10;i++){TenThread.Sleep (2000); OneSystem.out.println (Thread.CurrentThread (). GetName () + ":d One" +i); A             } -  - semaphore.release (); the}Catch(interruptedexception e) { - e.printstacktrace (); -         } -     } +  -      Public Static voidMain (string[] args) { +Signalnum signalnum=Newsignalnum (); AThread t1=NewThread (Signalnum, "T1"); atThread t2=NewThread (Signalnum, "T2"); -Thread t3=NewThread (Signalnum, "T3"); -  - T1.start (); - T2.start (); - T3.start (); in     } -}

This example specifies a semaphore that can run 2 at a time, after which the result is:

d:\java\jdk1.7.0_45\bin\java-didea.launcher.port=7535 "-didea.launcher.bin.path=d:\program Files (x86) \JetBrains\ IntelliJ idea 15.0.4\bin "-dfile.encoding=utf-8-classpath" d:\java\jdk1.7.0_45\jre\lib\charsets.jar;d:\java\ Jdk1.7.0_45\jre\lib\deploy.jar;d:\java\jdk1.7.0_45\jre\lib\ext\access-bridge-64.jar;d:\java\jdk1.7.0_45\jre\ Lib\ext\dnsns.jar;d:\java\jdk1.7.0_45\jre\lib\ext\jaccess.jar;d:\java\jdk1.7.0_45\jre\lib\ext\localedata.jar;d : \java\jdk1.7.0_45\jre\lib\ext\sunec.jar;d:\java\jdk1.7.0_45\jre\lib\ext\sunjce_provider.jar;d:\java\jdk1.7.0_ 45\jre\lib\ext\sunmscapi.jar;d:\java\jdk1.7.0_45\jre\lib\ext\zipfs.jar;d:\java\jdk1.7.0_45\jre\lib\javaws.jar ;D: \java\jdk1.7.0_45\jre\lib\jce.jar;d:\java\jdk1.7.0_45\jre\lib\jfr.jar;d:\java\jdk1.7.0_45\jre\lib\jfxrt.jar ;D: \java\jdk1.7.0_45\jre\lib\jsse.jar;d:\java\jdk1.7.0_45\jre\lib\management-agent.jar;d:\java\jdk1.7.0_45\jre \lib\plugin.jar;d:\java\jdk1.7.0_45\jre\lib\resources.jar;d:\java\jdk1.7.0_45\jre\lib\rt.jar; E:\JavaIDEA\TestJavaThread\Out\test\test;d:\program Files (x86) \jetbrains\intellij idea 15.0.4\lib\idea_rt.jar " Com.intellij.rt.execution.application.AppMain Signalnum
T1:done1
T2:done1
T1:done2
T2:done2
T1:done3
T2:done3
T1:done4
T2:done4
T1:done5
T2:done5
T1:done6
T2:done6
T1:done7
T2:done7
T1:done8
T2:done8
T1:done9
T2:done9
T3:done1
T3:done2
T3:done3
T3:done4
T3:done5
T3:done6
T3:done7
T3:done8
T3:done9

Process finished with exit code 0

We can see that T1 T2 two threads can execute and then T3 wait until two threads have finished executing.

The semaphore is used to synchronize the data.

But will the resources that are available at the same time be thread safe? I did the test so that 3 threads get the resources at the same time and then add 10,000 times each, and I found that there would be less than 30,000 times, stating that the thread that gets the resources at the same time is thread insecure, so even if the semaphore is used, the lock is added. Here I think it is more appropriate to use a re-entry lock, which is meaningless if you use synchronized to get resources at the same time.

Java Multithreaded Learning notes--use of 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.