<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FThreads%2FSemaphore</id>
		<title>Java/Threads/Semaphore - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FThreads%2FSemaphore"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Threads/Semaphore&amp;action=history"/>
		<updated>2026-04-24T15:34:40Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Threads/Semaphore&amp;diff=8327&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Threads/Semaphore&amp;diff=8327&amp;oldid=prev"/>
				<updated>2010-06-01T06:58:58Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:58, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

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

	</feed>