Java笔记-Semaphore简单应用实例
生活随笔
收集整理的這篇文章主要介紹了
Java笔记-Semaphore简单应用实例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這個(gè)Semaphore和QSemaphore使用和原理一模一樣。
這里簡(jiǎn)單寫了個(gè)例子,開了10個(gè)線程去拿鄰界資源,資源只有一個(gè),拿到后要進(jìn)行5s的業(yè)務(wù)錯(cuò)了,使用Semaphore,如果此資源被占用,那么該線程將會(huì)被掛起。
程序運(yùn)行截圖如下:
D:\java8\content\bin\java.exe -javaagent:D:\idea\content\lib\idea_rt.jar=52182:D:\idea\content\bin -Dfile.encoding=UTF-8 -classpath D:\java8\content\jre\lib\charsets.jar;D:\java8\content\jre\lib\deploy.jar;D:\java8\content\jre\lib\ext\access-bridge-64.jar;D:\java8\content\jre\lib\ext\cldrdata.jar;D:\java8\content\jre\lib\ext\dnsns.jar;D:\java8\content\jre\lib\ext\jaccess.jar;D:\java8\content\jre\lib\ext\jfxrt.jar;D:\java8\content\jre\lib\ext\localedata.jar;D:\java8\content\jre\lib\ext\nashorn.jar;D:\java8\content\jre\lib\ext\sunec.jar;D:\java8\content\jre\lib\ext\sunjce_provider.jar;D:\java8\content\jre\lib\ext\sunmscapi.jar;D:\java8\content\jre\lib\ext\sunpkcs11.jar;D:\java8\content\jre\lib\ext\zipfs.jar;D:\java8\content\jre\lib\javaws.jar;D:\java8\content\jre\lib\jce.jar;D:\java8\content\jre\lib\jfr.jar;D:\java8\content\jre\lib\jfxswt.jar;D:\java8\content\jre\lib\jsse.jar;D:\java8\content\jre\lib\management-agent.jar;D:\java8\content\jre\lib\plugin.jar;D:\java8\content\jre\lib\resources.jar;D:\java8\content\jre\lib\rt.jar;D:\IDEAProject\SemaphoreJavaDemo\target\classes cn.it1995.MyMain 2021-08-10 16:12:46->Thread[pool-1-thread-1,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:12:51->Thread[pool-1-thread-1,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:12:51->Thread[pool-1-thread-3,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:12:56->Thread[pool-1-thread-3,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:12:56->Thread[pool-1-thread-7,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:01->Thread[pool-1-thread-7,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:01->Thread[pool-1-thread-2,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:06->Thread[pool-1-thread-2,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:06->Thread[pool-1-thread-5,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:11->Thread[pool-1-thread-5,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:11->Thread[pool-1-thread-8,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:16->Thread[pool-1-thread-8,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:16->Thread[pool-1-thread-4,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:21->Thread[pool-1-thread-4,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:21->Thread[pool-1-thread-6,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:26->Thread[pool-1-thread-6,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:26->Thread[pool-1-thread-10,5,main] 開始處理業(yè)務(wù) 2021-08-10 16:13:31->Thread[pool-1-thread-10,5,main] 處理業(yè)務(wù)完成 2021-08-10 16:13:31->Thread[pool-1-thread-9,5,main] 開始處理業(yè)務(wù)Process finished with exit code -1程序結(jié)構(gòu)如下:
源碼如下:
MyMain.java
package cn.it1995;import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class MyMain {public static void main(String[] args) {ExecutorService threadPool = Executors.newCachedThreadPool();for(int i = 0; i < 10; i++){threadPool.execute(new MyThread());}} }MyResource.java
package cn.it1995;import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Semaphore;public class MyResource {private static Semaphore semaphore = new Semaphore(1);private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式public static void todoSth(){try {semaphore.acquire(1);Date startDate = new Date();String time = sdf.format(startDate);System.out.println(time + "->" + Thread.currentThread() + " 開始處理業(yè)務(wù)");Thread.sleep(5 * 1000);Date endDate = new Date();time = sdf.format(endDate);System.out.println(time + "->" + Thread.currentThread() + " 處理業(yè)務(wù)完成");}catch (Exception e){e.printStackTrace();}finally {semaphore.release(1);}} }?MyThread.java
package cn.it1995;public class MyThread implements Runnable {public void run() {while (true) {try {MyResource.todoSth();Thread.sleep( 1 * 1000);}catch (InterruptedException e) {e.printStackTrace();}}} }源碼打包下載地址:
Java/SemaphoreJavaDemo at master · fengfanchen/Java · GitHub
總結(jié)
以上是生活随笔為你收集整理的Java笔记-Semaphore简单应用实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring Boot笔记-JPA自定义
- 下一篇: Spring Boot笔记-接收参数的3