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

141 lines
5.5 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?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: Wait for threads using Java" />
<meta name="abstract" content="This example shows a Java program starting several threads, waiting for them to finish, and checking their status after completion." />
<meta name="description" content="This example shows a Java program starting several threads, waiting for them to finish, and checking their status after completion." />
<meta name="DC.Relation" scheme="URI" content="rzahwwatco.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="rzahwex3-ex3rx" />
<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: Wait for threads using Java</title>
</head>
<body id="rzahwex3-ex3rx"><a name="rzahwex3-ex3rx"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Wait for threads using Java</h1>
<div><p>This example shows a Java™ program starting several threads, waiting
for them to finish, and checking their status after completion.</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: ATEST15.java
The output of this example is as follows:
Entered the testcase
Create some worker threads
Start the thread
Thread Thread-1: Entered
Thread Thread-1: Working
Thread Thread-2: Entered
Thread Thread-2: Working
Thread Thread-3: Entered
Thread Thread-3: Working
Thread Thread-4: Entered
Thread Thread-4: Working
Wait for worker threads to complete
Thread Thread-5: Entered
Thread Thread-5: Working
Thread Thread-1: Done with work
Thread Thread-2: Done with work
Thread Thread-3: Done with work
Thread Thread-4: Done with work
Thread Thread-5: Done with work
Check all thread's results
Testcase completed
*/
import java.lang.*;
public class ATEST15 {
public final static int THREADGROUPSIZE = 5;
static class theThread extends Thread {
public final static int THREADPASS = 0;
public final static int THREADFAIL = 1;
int _status;
public int status() {
return _status;
}
public theThread() {
_status = THREADFAIL;
}
public void run() {
System.out.print("Thread " + getName() + ": Entered\n");
System.out.print("Thread " + getName() + ": Working\n");
safeSleep(15000, "Thread " + getName() + " work");
System.out.print("Thread " + getName() + ": Done with work\n");
_status = THREADPASS;
}
}
public static void main(String argv[]) {
int i=0;
theThread thread[] = new theThread[THREADGROUPSIZE];
System.out.print("Entered the testcase\n");
System.out.print("Create some worker threads\n");
System.out.print("Start the thread\n");
/* We won't use a ThreadGroup for this example, because we'd */
/* still have to join all the threads individually */
for (i=0; i &lt;THREADGROUPSIZE; ++i) {
thread[i] = new theThread();
thread[i].start();
}
System.out.print("Wait for worker threads to complete\n");
for (i=0; i &lt;THREADGROUPSIZE; ++i) {
try {
thread[i].join();
}
catch (InterruptedException e) {
System.out.print("Join interrupted\n");
}
}
System.out.print("Check all thread's results\n");
for (i=0; i &lt;THREADGROUPSIZE; ++1) {
if (thread[i].status() != theThread.THREADPASS) {
System.out.print("Unexpected thread status\n");
}
}
System.out.print("Testcase completed\n");
System.exit(0);
}
public static void safeSleep(long milliseconds, String s) {
try {
Thread.sleep(milliseconds);
}
catch (InterruptedException e) {
System.out.print(s);
}
}
}
</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwwatco.htm" title="When using threads, it is important to know when a thread has finished processing. Waiting for a thread to perform an action or an event to happen is called thread synchronization.">Wait for a thread to end</a></div>
</div>
</div>
</body>
</html>