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

132 lines
5.9 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 mutexes in Java" />
<meta name="abstract" content="This example shows a Java program creating a critical section of code. In Java, any object or array can function similarly to a mutual exclusion (mutex) using the synchronized keyword on a block of code or a method." />
<meta name="description" content="This example shows a Java program creating a critical section of code. In Java, any object or array can function similarly to a mutual exclusion (mutex) using the synchronized keyword on a block of code or a method." />
<meta name="DC.Relation" scheme="URI" content="rzahwmutco.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="rzahwex4-ex4rx" />
<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 mutexes in Java</title>
</head>
<body id="rzahwex4-ex4rx"><a name="rzahwex4-ex4rx"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Use mutexes in Java</h1>
<div><p>This example shows a Java™ program creating a critical section
of code. In Java, any object or array can function similarly to
a mutual exclusion (mutex) using the synchronized keyword on a block of code
or a method.</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: ATEST16.java
The output of this example is as follows:
Entered the testcase
Synchronize to prevent access to shared data
Create/start the thread
Thread Thread-1: Entered
Thread Thread-2: Entered
Wait a bit until we're 'done' with the shared data
Thread Thread-3: Entered
Unlock shared data
Thread Thread-1: Start critical section, in synchronized block
Thread Thread-1: End critical section, leave synchronized block
Thread Thread-2: Start critical section, in synchronized block
Thread Thread-2: End critical section, leave synchronized block
Thread Thread-3: Start critical section, in synchronized block
Thread Thread-3: End critical section, leave synchronized block
Wait for the threads to complete
Testcase completed
*/
import java.lang.*;
public class ATEST16 {
public final static int NUMTHREADS = 3;
public static int sharedData = 0;
public static int sharedData2 = 0;
/* Any real java object or array would suit for synchronization */
/* We invent one here since we have two unique data items to synchronize */
/* An in this simple example, they're not in an object */
static class theLock extends Object {
}
static public theLock lockObject = new theLock();
static class theThread extends Thread {
public void run() {
System.out.print("Thread " + getName() + ": Entered\n");
synchronized (lockObject) {
/********** Critical Section *******************/
System.out.print("Thread " + getName() +
": Start critical section, in synchronized block\n");
++sharedData; --sharedData2;
System.out.print("Thread " + getName() +
": End critical section, leave synchronized block\n");
/********** Critical Section *******************/
}
}
}
public static void main(String argv[]) {
theThread threads[] = new theThread[NUMTHREADS];
System.out.print("Entered the testcase\n");
System.out.print("Synchronize to prevent access to shared data\n");
synchronized (lockObject) {
System.out.print("Create/start the thread\n");
for (int i=0; i&lt;NUMTHREADS; ++i) {
threads[i] = new theThread();
threads[i].start();
}
System.out.print("Wait a bit until we're 'done' with the shared data\n");
try {
Thread.sleep(3000);
}
catch (InterruptedException e) {
System.out.print("sleep interrupted\n");
}
System.out.print("Unlock shared data\n");
}
System.out.print("Wait for the threads to complete\n");
for(int i=0; i &lt;NUMTHREADS; ++i) {
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="rzahwmutco.htm" title="A mutual exclusion (mutex) is used cooperatively between threads to ensure that only one of the cooperating threads is allowed to access the data or run certain application code at a time.">Mutexes and threads</a></div>
</div>
</div>
</body>
</html>