Java/Threads/Semaphore
Содержание
An implementation of a producer and consumer that use semaphores to control synchronization.
<source lang="java">
import java.util.concurrent.Semaphore; class Queue {
int value; static Semaphore semCon = new Semaphore(0); static Semaphore semProd = new Semaphore(1); void get() { try { semCon.acquire(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Got: " + value); semProd.release(); } void put(int n) { try { semProd.acquire(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } this.value = n; System.out.println("Put: " + n); semCon.release(); }
} class Producer implements Runnable {
Queue q; Producer(Queue q) { this.q = q; new Thread(this, "Producer").start(); } public void run() { for (int i = 0; i < 20; i++) q.put(i); }
} class Consumer implements Runnable {
Queue q; Consumer(Queue q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() { for (int i = 0; i < 20; i++) q.get(); }
} public class ProdCon {
public static void main(String args[]) { Queue q = new Queue(); new Consumer(q); new Producer(q); }
}
</source>
A semaphore
<source lang="java">
import java.util.concurrent.Semaphore; public class Main {
public static void main(String args[]) throws Exception { Semaphore sem = new Semaphore(1, true); Thread thrdA = new Thread(new SyncOutput(sem, "Message 1")); Thread thrdB = new Thread(new SyncOutput(sem, "Message 2!")); thrdA.start(); thrdB.start(); thrdA.join(); thrdB.join(); }
} class SyncOutput implements Runnable {
Semaphore sem; String msg; SyncOutput(Semaphore s, String m) { sem = s; msg = m; } public void run() { try { sem.acquire(); System.out.println(msg); Thread.sleep(10); } catch (Exception exc) { System.out.println("Error Writing File"); } sem.release(); }
}
</source>
Semaphore that can allow a specified number of threads to enter, blocking the others.
<source lang="java">
/*
* JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */
import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; /**
* Semaphore that can allow a specified number of threads to enter, blocking the * others. If the specified number of threads is 1, it acts as an exclusive * semaphore and can be used instead of synchronized blocks * * @author * @version $Revision: 2787 $ */
interface Sync {
/** * Acquires this sync * * @see #release * @throws InterruptedException */ void acquire() throws InterruptedException; /** * Releases this sync * * @see #acquire */ void release();
}
</source>
Wait exclusive semaphore with wait - notify primitives
<source lang="java">
/*
* JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */
import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; /*
* JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and * individual contributors as indicated by the @authors tag. See the * copyright.txt in the distribution for a full listing of individual * contributors. * * This is free software; you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This software is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this software; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF * site: http://www.fsf.org. */
/**
* Wait exclusive semaphore with wait - notify primitives * * @author * @version $Revision: 2787 $ */
interface WaitSync extends Sync {
/** * Pone in wait status this sync, until {@link #doNotify} is called to wake it * up. * * @see #doNotify * @throws InterruptedException */ void doWait() throws InterruptedException; /** * Wakes up this sync that has been posed in wait status by a {@link #doWait} * call. If this sync is not waiting, invoking this method should have no * effect. * * @see #doWait * @throws InterruptedException */ void doNotify() throws InterruptedException;
}
</source>