ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahw_5.4.0.1/rzahwex5rx.htm

176 lines
7.4 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Use condition variables in Java programs" />
<meta name="abstract" content="This example shows a Java program using condition variables in the form of the wait and notify methods on a Java object. Note the locking protocol used." />
<meta name="description" content="This example shows a Java program using condition variables in the form of the wait and notify methods on a Java object. Note the locking protocol used." />
<meta name="DC.Relation" scheme="URI" content="rzahwvarco.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rzahwex5-ex5rx" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Use condition variables in Java programs</title>
</head>
<body id="rzahwex5-ex5rx"><a name="rzahwex5-ex5rx"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Use condition variables in Java programs</h1>
<div><p>This example shows a Java™ program using condition variables in
the form of the wait and notify methods on a Java object. Note the locking protocol used.</p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
</div>
<div class="example"> <pre>/*
FileName: ATEST18.java
The output of this example is as follows:
Entered the testcase
Create/start the thread
Consumer Thread-1: Entered
Consumer Thread-1: Wait for the data to be produced
Producer: 'Finding data
Consumer Thread-2: Entered
Consumer Thread-2: Wait for the data to be produced
Producer: Make data shared and notify consumer
Producer: Unlock shared data and flag
Consumer Thread-2: Found data or notified, CONSUME IT while holding inside the monitor
Consumer Thread-2: Wait for the data to be produced
Producer: 'Finding data
Producer: Make data shared and notify consumer
Producer: Unlock shared data and flag
Producer: 'Finding data
Consumer Thread-2: Found data or notified, CONSUME IT while holding inside the monitor
Consumer Thread-2: All done
Producer: Make data shared and notify consumer
Producer: Unlock shared data and flag
Producer: 'Finding data
Consumer Thread-1: Found data or notified, CONSUME IT while holding inside the monitor
Consumer Thread-1: Wait for the data to be produced
Producer: Make data shared and notify consumer
Producer: Unlock shared data and flag
Wait for the threads to complete
Consumer Thread-1: Found data or notified, CONSUME IT while holding inside the monitor
Consumer Thread-1: All done
Testcase completed
*/
import java.lang.*;
/* This class is an encapsulation of the condition variable plus */
/* mutex locking logic that can be seen in the Pthread example */
class theSharedData extends Object {
int dataPresent;
int sharedData;
public theSharedData() {
dataPresent=0;
sharedData=0;
}
public synchronized void get() {
while (dataPresent == 0) {
try {
System.out.print("Consumer " +
Thread.currentThread().getName() +
": Wait for the data to be produced\n");
wait();
}
catch (InterruptedException e) {
System.out.print("Consumer " +
Thread.currentThread().getName() +
": wait interrupted\n");
}
}
System.out.print("Consumer " +
Thread.currentThread().getName() +
": Found data or notified, CONSUME IT " +
"while holding inside the monitor\n");
--sharedData;
if (sharedData == 0) {dataPresent=0;}
/* in a real world application, the actual data would be returned */
/* here */
}
public synchronized void put() {
System.out.print("Producer: Make data shared and notify consumer\n");
++sharedData;
dataPresent=1;
notify();
System.out.print("Producer: Unlock shared data and flag\n");
/* unlock occurs when leaving the synchronized method */
}
}
public class ATEST18 {
public final static int NUMTHREADS = 2;
public static theSharedData dataConditionEncapsulation = new theSharedData();
static class theThread extends Thread {
public void run() {
int retries=2;
System.out.print("Consumer " + getName() + ": Entered\n");
while (retries-- != 0) {
dataConditionEncapsulation.get();
/* Typically an application would process the data outside */
/* the monitor (synchronized get method here) */
}
System.out.print("Consumer " + getName() + ": All done\n");
}
}
public static void main(String argv[]) {
int amountOfData = 4;
theThread threads[] = new theThread[NUMTHREADS];
System.out.print("Entered the testcase\n");
System.out.print("Create/start the thread\n");
for (int i=0; i &lt;NUMTHREADS; ++i) {
threads[i] = new theThread();
threads[i].start();
}
while (amountOfData-- != 0) {
System.out.print("Producer: 'Finding data\n");
try {
Thread.sleep(3000);
}
catch (InterruptedException e) {
System.out.print("sleep interrupted\n");
}
dataConditionEncapsulation.put();
}
System.out.print("Wait for the threads to complete\n");
for(int i=0; i &lt;NUMTHREADS; ++i) {
try {
threads[i].join();
}
catch (InterruptedException e) {
System.out.print("Join interrupted\n");
}
}
System.out.print("Testcase completed\n");
System.exit(0);
}
}
</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwvarco.htm" title="Condition variables allow threads to wait for certain events or conditions to occur and they notify other threads that are also waiting for the same events or conditions.">Condition variables and threads</a></div>
</div>
</div>
</body>
</html>