<?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_Tutorial%2FThread%2FThread_Buffer</id>
		<title>Java Tutorial/Thread/Thread Buffer - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FThread%2FThread_Buffer"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Buffer&amp;action=history"/>
		<updated>2026-04-21T22:30:40Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Buffer&amp;diff=2773&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Buffer&amp;diff=2773&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://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Buffer&amp;diff=2774&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Thread/Thread_Buffer&amp;diff=2774&amp;oldid=prev"/>
				<updated>2010-05-31T15:18:05Z</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;==  Two threads manipulating an unsynchronized buffer ==&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.ExecutorService;&lt;br /&gt;
import java.util.concurrent.Executors;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ExecutorService application = Executors.newFixedThreadPool(2);&lt;br /&gt;
    Buffer sharedLocation = new UnsynchronizedBuffer();&lt;br /&gt;
    System.out.println(&amp;quot;Action\t\tValue\tProduced\tConsumed&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;------\t\t-----\t--------\t--------\n&amp;quot;);&lt;br /&gt;
    try {&lt;br /&gt;
      application.execute(new Producer(sharedLocation));&lt;br /&gt;
      application.execute(new Consumer(sharedLocation));&lt;br /&gt;
    } catch (Exception exception) {&lt;br /&gt;
      exception.printStackTrace();&lt;br /&gt;
    }&lt;br /&gt;
    application.shutdown(); // terminate application when threads end&lt;br /&gt;
  } // end main&lt;br /&gt;
}&lt;br /&gt;
interface Buffer {&lt;br /&gt;
  public void set(int value);&lt;br /&gt;
  public int get();&lt;br /&gt;
}&lt;br /&gt;
// UnsynchronizedBuffer represents a single shared integer.&lt;br /&gt;
class UnsynchronizedBuffer implements Buffer {&lt;br /&gt;
  private int buffer = -1; // shared by producer and consumer threads&lt;br /&gt;
  public void set(int value) {&lt;br /&gt;
    System.out.printf(&amp;quot;Producer writes\t%2d&amp;quot;, value);&lt;br /&gt;
    buffer = value;&lt;br /&gt;
  }&lt;br /&gt;
  public int get() {&lt;br /&gt;
    System.out.printf(&amp;quot;Consumer reads\t%2d&amp;quot;, buffer);&lt;br /&gt;
    return buffer;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class Producer implements Runnable {&lt;br /&gt;
  private Buffer buffer; // reference to shared object&lt;br /&gt;
  public Producer(Buffer shared) {&lt;br /&gt;
    buffer = shared;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    for (int i = 1; i &amp;lt;= 10; i++) {&lt;br /&gt;
      try&lt;br /&gt;
      {&lt;br /&gt;
        Thread.sleep(1000);&lt;br /&gt;
        buffer.set(i);&lt;br /&gt;
        sum += i;&lt;br /&gt;
        System.out.printf(&amp;quot;\t%2d\n&amp;quot;, sum);&lt;br /&gt;
      } catch (InterruptedException exception) {&lt;br /&gt;
        exception.printStackTrace();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.printf(&amp;quot;\n%s\n%s\n&amp;quot;, &amp;quot;Producer done producing.&amp;quot;, &amp;quot;Terminating Producer.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class Consumer implements Runnable {&lt;br /&gt;
  private Buffer buffer;&lt;br /&gt;
  public Consumer(Buffer shared) {&lt;br /&gt;
    buffer = shared;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    for (int i = 1; i &amp;lt;= 10; i++) {&lt;br /&gt;
      try {&lt;br /&gt;
        Thread.sleep(1000);&lt;br /&gt;
        sum += buffer.get();&lt;br /&gt;
        System.out.printf(&amp;quot;\t\t\t%2d\n&amp;quot;, sum);&lt;br /&gt;
      } catch (InterruptedException exception) {&lt;br /&gt;
        exception.printStackTrace();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.out.printf(&amp;quot;\n%s %d.\n%s\n&amp;quot;, &amp;quot;Consumer read values totaling&amp;quot;, sum,&lt;br /&gt;
        &amp;quot;Terminating Consumer.&amp;quot;);&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;
&amp;lt;pre class=codeResult&amp;gt;Action		Value	Produced	Consumed&lt;br /&gt;
------		-----	--------	--------&lt;br /&gt;
Producer writes	 1Consumer reads	-1	 1&lt;br /&gt;
			 1&lt;br /&gt;
Consumer reads	 1Producer writes	 2			 2&lt;br /&gt;
	 3&lt;br /&gt;
Consumer reads	 2			 4&lt;br /&gt;
Producer writes	 3	 6&lt;br /&gt;
Consumer reads	 3			 7&lt;br /&gt;
Producer writes	 4	10&lt;br /&gt;
Consumer reads	 4			11&lt;br /&gt;
Producer writes	 5	15&lt;br /&gt;
Consumer reads	 5			16&lt;br /&gt;
Producer writes	 6	21&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>