マルチスレッド ch01 最大でN個のスレッドまでが実行するパターン
import java.util.Random;
import java.util.concurrent.Semaphore;
class Log {
public static void println(String s) {
System.out.println(Thread.currentThread().getName() + ": " + s);
}
}
// 数の制限があるリソース
class BoundedResource {
private final Semaphore semaphore;
private final int permits;
private final static Random random = new Random(314159);
// permitはリソースの個数
public BoundedResource(int permits) {
this.semaphore = new Semaphore(permits);
this.permits = permits;
}
// リソースを使用する
public void use() throws InterruptedException {
semaphore.acquire();
try {
doUse();
} finally {
semaphore.release();
}
}
// リソースを実際に使用する(ここではThread.sleepしているだけ)
protected void doUse() throws InterruptedException {
Log.println("BEGIN: used = " + (permits - semaphore.availablePermits()));
Thread.sleep(random.nextInt(500));
Log.println("END: used = " + (permits - semaphore.availablePermits()));
}
}
// リソースを利用するスレッド
class UserThread extends Thread {
private final static Random random = new Random(26535);
private final BoundedResource resource;
public UserThread(BoundedResource resource) {
this.resource = resource;
}
public void run() {
try {
while (true) {
resource.use();
Thread.sleep(random.nextInt(3000));
}
} catch (InterruptedException e) {
}
}
}
public class Main {
public static void main(String[] args) {
// 3個のリソースを用紙
BoundedResource resource = new BoundedResource(3);
// 10個のスレッドが利用する
for (int i = 0; i < 10; i++) {
new UserThread(resource).start();
}
}
}