Thread , .
Thread:
- MIN_PRIORITY . JVM. 1.
- NORM_PRIORITY - . , . 5.
- MAX_PRIORITY . 10.
- static int activeCount() - .
- static Thread currentThread() .
- boolean holdsLock(Object obj) true , - ( ) obj.
- static boolean interrupted() , false.
Thread:
- void run() , . Thread , . run() .
- void start() , run() . .
- void setDaemon(boolean on) on==true , .
- boolean isDaemon() - true , .
- void yield() , .
- long getId() . - () .
- String getName() , setName.
- void setName(String name) .
- int getPriority() - .
- void setPriority(int newPriority) .
- void checkAccess() . , , , . SecurityException.
- String toString() , , , .
- void sleep(long millis) () millis . () . sleep(long millis,int nanos)- nanos . interrupt() InterruptedException.
- void interrupt() , wait() sleep(), ( =true). InterruptedException.
- boolean isInterrupted() - .
- void join() . - (). join() - , join() . . join(long millis) - millis . join(0) join(). join(long millis,int nanos)- nanos . interrupt() InterruptedException. join() join UNIX. ( ).
- boolean isAlive() - true , ( ). , , -, isAlive() false , .
|
|
, . , , . . Java . Java, , - , - .
, . , , , . - , , . , .
, , 9 [8].
DLL. (native) *
*-
Java . . , . Java , native . , , - - . Java, - , (DLL Dynamic Link Library Windows, Shared Objects UNIX- ).
|
|
JNI Java Native Interface.
Java
native ( );
, :
System.loadLibrary();
. , Windows myLib.dll, UNIX Linux myLib.so, System.loadLibrary(myLib);
, , UnsatisfiedLinkError.
,
System.load ();
loadLibrary.
, , .
, , ++. JDK javah.exe, C++ Java. , , ClassWithNativeMethod.
:
package java_example_pkg;
public class ClassWithNativeMethod {
/** Creates a new instance of ClassWithNativeMethod */
public ClassWithNativeMethod() {
}
public native void myNativeMethod();
}
, javah, build\classes. java_example_pkg META-INF.
javah.exe java_example_pkg.ClassWithNativeMethod
- , . java_example_pkg_ClassWithNativeMethod.h :
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class java_example_pkg_ClassWithNativeMethod */
#ifndef _Included_java_example_pkg_ClassWithNativeMethod
#define _Included_java_example_pkg_ClassWithNativeMethod
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: java_example_pkg_ClassWithNativeMethod
* Method: myNativeMethod
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_java_1example_1pkg_ClassWithNativeMethod_myNativeMethod
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Java_java_1example_1pkg_ClassWithNativeMethod_myNativeMethod(JNIEnv *, jobject), C++, myNativeMethod() Java. C++ : Java, _, ( _ _1), _, , _, .
JNIEnv * C++ , C++ Java. , . , jobject, , , , jclass . C++ , Java . , .
|
|
, C++ . ,
public native int myNativeMethod(int i);
C++
(JNIEnv *, jobject, jint)
jint void.
Java C++:
Java | JNI (C++) | JNI |
boolean | jboolean | 1 , |
byte | jbyte | 1 |
char | jchar | 2 , |
short | jshort | 2 |
int | jint | 4 |
long | jlong | 8 |
float | jfloat | 4 |
double | jdouble | 8 |
void | void | - |
Object | jobject | |
Class | jclass | Java |
String | jstring | Java |
jarray | ||
Object[] | jobjectArray | |
boolean[] | jbooleanArray | |
byte[] | jbyteArray | ( ) |
char[] | jcharArray | |
short[] | jshortArray | |
int[] | jintArray | |
long[] | jlongArray | |
float[] | jfloatArray | float |
double[] | jdoubleArray | double |
Throwable | jthrowable |
. , , ( ), C++ :
#include java_example_pkg_ClassWithNativeMethod.h
JNIEXPORT jint JNICALL Java_java_1example_1pkg_ClassWithNativeMethod_myNativeMethod
(JNIEnv *env, jobject obj, jint i){
return i*i
};
, env. , , jintArray intArr, :
jsize length=(*env)->GetArrayLength(env, intArr);
:
jint *intArrRef=(*env)->GetIntArrayElements(env, intArr,0);
intArr C++.
- :
(*env)->ReleaseIntArrayElements(env, intArr, intArrRef,0);
:
GetBooleanArrayElements, GetByteArrayElements,, GetDoubleArrayElements. Java , C++. GetObjectArrayElement, SetObjectArrayElement .
Java jstring s C++ :
const char *sRef=(*env)->GetStringUTFChars(env,s,0);
Ÿ int s_len=strlen(sRef);
(*env)->ReleaseStringUTFChars(env,s,sRef);
9
ü , , (process), , , . . (threads) , . (), , , . , .
|
|
ü () . . -, .
ü Java . Java- (JVM) main , , . main.
ü , , . , . . , . Java synchronize (). ( ) .
ü , , . , , .
ü : .
obj1 : synchronized(obj1) ;
synchronized :
public synchronized (...){...}
ü , , . wait () notify ().
ü , : java.lang.Thread java.lang.Runnable.
ü java.lang.Runnable public void run(), . Thread Runnable, run(). Thread run().
ü . TreadGroup ( ).
ü , , Java , native . , - - . Java, - , .
- , . Thread. run 1 100 10 , , Thread 1: , Thread 2: , .. print10. , .
- , print10 run .
- , , 101 200, Runnable, print10 . .
10.