ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/nmthread.htm

105 lines
6.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="concept" />
<meta name="DC.Title" content="Java native methods and threads considerations" />
<meta name="abstract" content="You can use native methods to access functions that are not available in Java. To better use Java with native methods, you need to understand these concepts." />
<meta name="description" content="You can use native methods to access functions that are not available in Java. To better use Java with native methods, you need to understand these concepts." />
<meta name="DC.Relation" scheme="URI" content="jni.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="nmthread" />
<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>Java native methods and threads considerations</title>
</head>
<body id="nmthread"><a name="nmthread"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Java native methods and threads considerations</h1>
<div><p>You can use native methods to access functions that are not available
in Java™.
To better use Java with native methods, you need to understand these
concepts.</p>
<ul><li>A Java thread,
whether created by Java or an attached native thread, has all
floating point exceptions disabled. If the thread runs a native method that
reenables floating point exceptions, Java does not turn them off a second time.
If the user application does not disable them before returning to run Java code,
then the Java code may not behave correctly if a floating point
exception occurs. When a native thread detaches from the Java virtual
machine, its floating point exception mask is restored to the value that was
in effect when it was attached.</li>
<li>When a native thread attaches to the Java virtual machine, the Java virtual
machine changes the threads priority, if necessary, to conform to the one
to ten priority schemes that Java defines. When the thread detaches,
the priority is restored. After attaching, the thread can change the thread
priority by using a native method interface (for example, a POSIX API). Java does
not change the thread priority on transitions back to the Java virtual
machine.</li>
<li>The Invocation API component of the Java Native Interface (JNI) permits a user
to embed a Java virtual machine within their application. If an
application creates a Java virtual machine and the Java virtual
machine ends abnormally, the MCH74A5 "Java Virtual Machine Terminated" iSeries™ exception
is signalled to the initial thread of the process if that thread was attached
to the Java virtual machine when the Java virtual
machine ended. The Java virtual machine could end abnormally
for any of these reasons: <ul><li>The user calls the java.lang.System.exit() method.</li>
<li>A thread that the Java virtual machine requires ends.</li>
<li>An internal error occurs in the Java virtual machine.</li>
</ul>
This behavior differs from most other Java platforms. On most other platforms,
the process that automatically creates the Java virtual machine ends abruptly as soon
as the Java virtual machine ends. If the application monitors
and handles a signalled MCH74A5 exception, it may continue to run. Otherwise,
the process ends when the exception goes unhandled. By adding the code that
deals with the iSeries server-specific
MCH74A5 exception, you can make the application less portable to other platforms.</li>
</ul>
<p>Because native methods always run in a multithreaded process, the code
that they contain must be thread safe. This places these restrictions on the
languages and functions that are used for native methods:</p>
<ul><li>You should not use ILE CL for native methods, because this language is
not thread safe. To run thread safe CL commands, you can use the C language
system() function or the java.lang.Runtime.exec() method. <ul><li>Use the C language system() function to run thread safe CL commands from
within a C or C++ native method.</li>
<li>Use the java.lang.Runtime.exec() method to run thread safe CL commands
directly from Java.</li>
</ul>
</li>
<li>You can use ILE C, ILE C++, ILE COBOL, and ILE RPG to write a native method,
but all of the functions that are called from within the native method must
be thread safe. <p><strong>Note:</strong> Compile-time support for writing native methods
is currently only supplied for the C, C++, and RPG languages. While possible,
writing native methods in other languages may be much more complicated.</p>
<p><strong>Caution: <em>Not
all standard C, C++, COBOL, or RPG functions are thread safe.</em></strong></p>
</li>
<li>The C and C++ exit() and abort() functions should never be used within
a native method. These functions cause the entire process that runs the Java virtual
machine to stop. This includes all of the threads in the process, regardless
of if they were originated by Java or not. <div class="note"><span class="notetitle">Note:</span> The exit() function
referred to is the C and C++ function, and is not the same as the java.lang.Runtime.exit()
method.</div>
</li>
</ul>
<p>For more information about threads on the iSeries server, see <a href="../rzahw/rzahwovepo.htm" target="_blank">Multithreaded applications</a>. </p>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="jni.htm" title="You should only use native methods in cases where pure Java cannot meet your programming needs.">Use the Java Native Interface for native methods</a></div>
</div>
</div>
</body>
</html>