java并发信号量Semaphore
2017-11-10 15:18
316 查看
package com.blocking;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class ArrayCopy {
public static void main(String[] args) {
Semaphore semaphore=new Semaphore(2);
ExecutorService se = Executors.newCachedThreadPool();
ArrayCopy arrayCopy=new ArrayCopy();
se.submit(arrayCopy.new SemaphoreThread(semaphore, "a"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "b"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "c"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "d"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "e"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "f"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "g"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "h"));
se.shutdown();
}
// public static SemaphoreThread getInstance(Semaphore semaphore,String id){
// return new SemaphoreThread(semaphore, id);
// }
class SemaphoreThread implements Runnable{
Semaphore semaphore;
String id;
public SemaphoreThread(Semaphore semaphore,String id) {
this.semaphore=semaphore;
this.id=id;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println("Thread:"+id+" acquire……");
Thread.sleep(2000);
semaphore.release();
System.out.println("Thread:"+id+" release……");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class ArrayCopy {
public static void main(String[] args) {
Semaphore semaphore=new Semaphore(2);
ExecutorService se = Executors.newCachedThreadPool();
ArrayCopy arrayCopy=new ArrayCopy();
se.submit(arrayCopy.new SemaphoreThread(semaphore, "a"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "b"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "c"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "d"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "e"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "f"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "g"));
se.submit(arrayCopy.new SemaphoreThread(semaphore, "h"));
se.shutdown();
}
// public static SemaphoreThread getInstance(Semaphore semaphore,String id){
// return new SemaphoreThread(semaphore, id);
// }
class SemaphoreThread implements Runnable{
Semaphore semaphore;
String id;
public SemaphoreThread(Semaphore semaphore,String id) {
this.semaphore=semaphore;
this.id=id;
}
@Override
public void run() {
try {
semaphore.acquire();
System.out.println("Thread:"+id+" acquire……");
Thread.sleep(2000);
semaphore.release();
System.out.println("Thread:"+id+" release……");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- java并发之(4):Semaphore信号量、CounDownLatch计数锁存器和CyclicBarrier循环栅栏
- 【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)(r)
- 体验 Java 并发 api,用不同方式实现信号量锁(Semaphore)(7)
- Java并发系列3--信号量Semaphore
- Java并发包之Semaphore信号量
- 【Java并发编程】并发新特性—信号量Semaphore(含代码)
- 【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)
- 转:【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)
- java并发编程学习:用 Semaphore (信号量)控制并发资源
- 【Java并发编程】:并发新特性—信号量Semaphore
- JAVA并发信号量 Semaphore
- Java多线程/并发22、信号量Semaphore
- Java并发学习笔记(15)信号量(Semaphore) 关卡((2)CyclicBarrier)
- 体验 Java 并发 api,用不同方式实现信号量锁(Semaphore)(1)
- JAVA并发编程-障碍器CyclicBarrier,计数器CountDownLatch,信号量Semaphore
- java 并发工具包 -信号量 Semaphore
- 体验 Java 并发 api,用不同方式实现信号量锁(Semaphore)(2)
- Java并发-类库新组件 - Semaphore 理解:计数信号量
- 体验 Java 并发 api,用不同方式实现信号量锁(Semaphore)(3)
- 【Java并发编程】之二十三:并发新特性—信号量Semaphore(含代码)