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

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Thread/BlockingQueue&amp;diff=2781&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Thread/BlockingQueue&amp;diff=2781&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:26Z</updated>
		
		<summary type="html">&lt;p&gt;&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;Версия 17:44, 31 мая 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>
			</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Thread/BlockingQueue&amp;diff=2782&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Thread/BlockingQueue&amp;diff=2782&amp;oldid=prev"/>
				<updated>2010-05-31T15:18:07Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Communicate between threads using a Queue ==&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;
import java.util.concurrent.BlockingQueue;&lt;br /&gt;
import java.util.concurrent.LinkedBlockingQueue;&lt;br /&gt;
class PrepareProduction implements Runnable {&lt;br /&gt;
  BlockingQueue&amp;lt;String&amp;gt; queue;&lt;br /&gt;
  PrepareProduction(BlockingQueue&amp;lt;String&amp;gt; q) {&lt;br /&gt;
    queue = q;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    String thisLine;&lt;br /&gt;
    try {&lt;br /&gt;
      queue.put(&amp;quot;1&amp;quot;);&lt;br /&gt;
      queue.put(&amp;quot;done&amp;quot;);&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      e.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class DoProduction implements Runnable {&lt;br /&gt;
  private final BlockingQueue&amp;lt;String&amp;gt; queue;&lt;br /&gt;
  DoProduction(BlockingQueue&amp;lt;String&amp;gt; q) {&lt;br /&gt;
    queue = q;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      String value = queue.take();&lt;br /&gt;
      while (!value.equals(&amp;quot;done&amp;quot;)) {&lt;br /&gt;
        value = queue.take();&lt;br /&gt;
        System.out.println(value);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (Exception e) {&lt;br /&gt;
      System.out.println(Thread.currentThread().getName() + &amp;quot; &amp;quot;&lt;br /&gt;
          + e.getMessage());&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    BlockingQueue&amp;lt;String&amp;gt; q = new LinkedBlockingQueue&amp;lt;String&amp;gt;();&lt;br /&gt;
    Thread p1 = new Thread(new PrepareProduction(q));&lt;br /&gt;
    Thread c1 = new Thread(new DoProduction(q));&lt;br /&gt;
    p1.start();&lt;br /&gt;
    c1.start();&lt;br /&gt;
    p1.join();&lt;br /&gt;
    c1.join();&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;
==  Creating a Bounded Work Queue ==&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;
import java.util.concurrent.ArrayBlockingQueue;&lt;br /&gt;
import java.util.concurrent.BlockingQueue;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    int capacity = 10;&lt;br /&gt;
    BlockingQueue&amp;lt;Integer&amp;gt; queue = new ArrayBlockingQueue&amp;lt;Integer&amp;gt;(capacity);&lt;br /&gt;
    int numWorkers = 2;&lt;br /&gt;
    Worker[] workers = new Worker[numWorkers];&lt;br /&gt;
    for (int i = 0; i &amp;lt; workers.length; i++) {&lt;br /&gt;
      workers[i] = new Worker(queue);&lt;br /&gt;
      workers[i].start();&lt;br /&gt;
    }&lt;br /&gt;
    for (int i = 0; i &amp;lt; 100; i++) {&lt;br /&gt;
      queue.put(i);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class Worker extends Thread {&lt;br /&gt;
  BlockingQueue&amp;lt;Integer&amp;gt; q;&lt;br /&gt;
  Worker(BlockingQueue&amp;lt;Integer&amp;gt; q) {&lt;br /&gt;
    this.q = q;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        Integer x = q.take();&lt;br /&gt;
        if (x == null) {&lt;br /&gt;
          break;&lt;br /&gt;
        }&lt;br /&gt;
        System.out.println(x);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&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;
==  Implementing an Unbounded Work Queue with synchronized block ==&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;
import java.util.LinkedList;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) {&lt;br /&gt;
    WorkQueue queue = new WorkQueue();&lt;br /&gt;
    int numWorkers = 2;&lt;br /&gt;
    Worker[] workers = new Worker[numWorkers];&lt;br /&gt;
    for (int i = 0; i &amp;lt; workers.length; i++) {&lt;br /&gt;
      workers[i] = new Worker(queue);&lt;br /&gt;
      workers[i].start();&lt;br /&gt;
    }&lt;br /&gt;
    for (int i = 0; i &amp;lt; 100; i++) {&lt;br /&gt;
      queue.addWork(i);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class WorkQueue {&lt;br /&gt;
  LinkedList&amp;lt;Object&amp;gt; queue = new LinkedList&amp;lt;Object&amp;gt;();&lt;br /&gt;
  public synchronized void addWork(Object o) {&lt;br /&gt;
    queue.addLast(o);&lt;br /&gt;
    notify();&lt;br /&gt;
  }&lt;br /&gt;
  public synchronized Object getWork() throws InterruptedException {&lt;br /&gt;
    while (queue.isEmpty()) {&lt;br /&gt;
      wait();&lt;br /&gt;
    }&lt;br /&gt;
    return queue.removeFirst();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class Worker extends Thread {&lt;br /&gt;
  WorkQueue q;&lt;br /&gt;
  Worker(WorkQueue q) {&lt;br /&gt;
    this.q = q;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      while (true) {&lt;br /&gt;
        Object x = q.getWork();&lt;br /&gt;
        if (x == null) {&lt;br /&gt;
          break;&lt;br /&gt;
        }&lt;br /&gt;
        System.out.println(x);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&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>
		<author><name>Admin</name></author>	</entry>

	</feed>