我看JAVA 之 Object & JNI
我看JAVA 之 Object & JNI
注:基于jdk11
Object
Object类是java语言中所有类的父类。
public class Object {
//注册本地函数,方可以从库中的本机代码调用JNI函数
private static native void registerNatives();
static {
registerNatives();
}
/**
* Constructs a new object.
*/
//HotSpotIntrinsicCandidate注解,说明本方法,jvm额外维护了一个利用了CPU指令的高效实现
@HotSpotIntrinsicCandidate
public Object() {}
//返回对象的运行时类型
/**
* Number c = 0;
* Class<? extends Number> clazz = c.getClass();
* System.out.println(clazz);
* 打印如下:
* class java.lang.Integer
* 关于泛型及泛型擦除概念,见后续泛型章节
*/
@HotSpotIntrinsicCandidate
public final native Class<?> getClass();
/**
* 返回对象的hash码,hashCode方法支持hash表(比如java.util.HashMap)的特性,
* 不同的对象返回唯一的has码,可以提高hash表数据结构的性能。
* hashCode有时候以对象内存地址的机制生成,有时候不是
* java.lang.System.identityHashCode 工具方法 返回默认hashCode()方法的值,null引用返回的hash码为0
*/
@HotSpotIntrinsicCandidate
public native int hashCode();
/**
* 标示其他对象与当前对象是否相等
* 自反性: 自己(非null)与自己比较永远相当
* 对称性: x vs y or y vs x 等价
* 可传递: x.equals(y) == true and y.equals(z), then x.equals(z)
* 一致性: 一旦x.equals(y) == true, then anytime x.equals(y) == true
* any not null value not equals null
* 如果覆盖equals方法,一定要覆盖hashCode方法
*/
public boolean equals(Object obj) {
return (this == obj);
}
/**
* clone返回会返回一个相同类型的新对象拷贝,如果要实现克隆,那么当前类及其父类需要实现cloneable接口。
* 对于复杂类型,默认的clone方式仅实现”浅克隆”,如果要实现”深克隆”,需要覆盖clone方法。
* 注:在软件编程中,推荐使用工具类的方式做copy,而不是覆盖clone的方式
*/
@HotSpotIntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;
/**
* 默认返回 getClass().getName() + '@' + Integer.toHexString(hashCode())
* 具体类可以覆盖Object的toString()方法
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
/**
* 持有该对象锁的线程通过调用notify唤醒等待此对象监视器的某一个线程,选择是随机的(具体由不同虚拟机实现)。一次只能有一个线程持有对象锁。
* 获取对象锁定三种方式如下:
* 1,通过执行该对象的同步实例方法
* 2,通过执行该对象的同步代码块
* 3,通过执行类的同步静态方法
* 见 /openjdk/hotspot/src/share/vm/prims/jvm.cpp line: 526~530
* 见 /openjdk/hotspot/src/share/vm/runtime/synchronizer.cpp line: 407~411
* @throws IllegalMonitorStateException if the current thread is not
* the owner of this object's monitor.
* @see java.lang.Object#notifyAll()
* @see java.lang.Object#wait()
*/
@HotSpotIntrinsicCandidate
public final native void notify();
/**
* 持有该对象锁的线程通过调用notify唤醒等待此对象监视器的所有线程。当当前线程释放对象监视器后,其他唤醒的线程会竞争对象监视器。
*
* @throws IllegalMonitorStateException if the current thread is not
* the owner of this object's monitor.
* @see java.lang.Object#notify()
* @see java.lang.Object#wait()
*/
@HotSpotIntrinsicCandidate
public final native void notifyAll();
/**
* 调用此方法会导致当前对象进入等待状态,知道被其他线程唤醒(notify)或中断(InterruptedException)。相当于wait(0L)
*
* @throws IllegalMonitorStateException if the current thread is not
* the owner of the object's monitor
* @throws InterruptedException if any thread interrupted the current thread before or
* while the current thread was waiting. The <em>interrupted status</em> of the
* current thread is cleared when this exception is thrown.
* @see #notify()
* @see #notifyAll()
* @see #wait(long)
* @see #wait(long, int)
*/
public final void wait() throws InterruptedException {
wait(0L);
}
/**
*
* @param timeoutMillis the maximum time to wait, in milliseconds
* @throws IllegalArgumentException if {@code timeoutMillis} is negative
* @throws IllegalMonitorStateException if the current thread is not
* the owner of the object's monitor
* @throws InterruptedException if any thread interrupted the current thread before or
* while the current thread was waiting. The <em>interrupted status</em> of the
* current thread is cleared when this exception is thrown.
* @see #notify()
* @see #notifyAll()
* @see #wait()
* @see #wait(long, int)
*/
public final native void wait(long timeoutMillis) throws InterruptedException;
/**
* 在并发编程章节重点讲解
* @param timeoutMillis the maximum time to wait, in milliseconds
* @param nanos additional time, in nanoseconds, in the range range 0-999999 inclusive
* @throws IllegalArgumentException if {@code timeoutMillis} is negative,
* or if the value of {@code nanos} is out of range
* @throws IllegalMonitorStateException if the current thread is not
* the owner of the object's monitor
* @throws InterruptedException if any thread interrupted the current thread before or
* while the current thread was waiting. The <em>interrupted status</em> of the
* current thread is cleared when this exception is thrown.
* @see #notify()
* @see #notifyAll()
* @see #wait()
* @see #wait(long)
*/
public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
if (timeoutMillis < 0) {
throw new IllegalArgumentException("timeoutMillis value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeoutMillis++;
}
wait(timeoutMillis);
}
/**
* 通知垃圾回收器当前对应已经没有引用了,具体回收还要看jvm内部实现。
* jdk9 标记位过时,在jvm原理章节重点讲解
*
* @throws Throwable the {@code Exception} raised by this method
* @see java.lang.ref.WeakReference
* @see java.lang.ref.PhantomReference
* @jls 12.6 Finalization of Class Instances
*/
@Deprecated(since="9")
protected void finalize() throws Throwable { }
}
JNI(Java Native Interface)
java本地接口,意为JAVA语言提供一套规范供操作系统底层实现,一般是C/C++语言,不同操作系统版本的jvm提供了相应的实现,以达到"Write Once Run Anywhere“
实现JNI大致步骤如下:
-
定义java程序
package chapter01; public class TestJNI { public native void hello();//所有native关键词修饰的都是对本地的声明 static { System.loadLibrary("hello");//载入本地库 } public static void main(String[] args) { new TestJNI().hello(); } }
- 编译javac chapter01/TestJNI.java,生成TestJNI.class
-
执行javac chapter01/TestJNI.java -h . ,生成JAVA本地接口chapter01_TestJNI.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class chapter01_TestJNI */ #ifndef _Included_chapter01_TestJNI #define _Included_chapter01_TestJNI #ifdef __cplusplus extern "C" { #endif /* * Class: chapter01_TestJNI * Method: hello * Signature: ()V */ JNIEXPORT void JNICALL Java_chapter01_TestJNI_hello (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
注意:JNIEnv、 jobject等类型都是在jni.h头文件中定义的,所以需要include jni.h
-
编写步骤3的接口实现,创建TestJNIImpl.c
#include "jni.h" #include "chapter01_TestJNI.h" //#include otherheaders JNIEXPORT void JNICALL Java_chapter01_TestJNI_hello(JNIEnv *env, jobject obj) { printf("Helloworld!\n"); return; }
- 将本地方法编写的文件生成动态链接库
gcc -dynamiclib -I /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include TestJNIImpl.c -o libhello.jnilib
注意:如果/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include不存在jni_md.h的话,可以从 /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include/darwin 拷贝过来 - 执行java程序 java -Djava.library.path=/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/include chapter01/TestJNI

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
对话阿里云叔同:释放云价值,让容器成为“普适”技术
作者 | 刘丹受访者 | 丁宇出品 | CSDN云计算(ID:CSDNcloud) 云改变了IT业态和市场格局,催生了应用大发展的时代,企业可以更加专注于构建符合其愿景的、更具生命力的业务创新。全面使用云服务构建软件的时代已经到来,在这个大背景下,云原生的概念被提出并迅速具象化,而以容器为代表的云原生技术,作为提升云化服务能力的最佳选择,也得以快速发展。 2019年1月份,Tripwire发布了2019年关于容器的最新现状调查研究。根据反馈,高达86%的受访者在生产环境中使用了容器,近32%的受访者所在企业生产环境中使用容器的数量超过了100个。整体来看,容器在生产环境中的使用量已经得到了很大程度的提升。调查预测显示,目前应用容器市场规模将从2016年的7.62亿美元增长到2020年的27亿美元。不难看出,引入容器所展现的巨大灵活性有效地推动了其应用的步伐,使企业日益依赖该技术。 近年来,容器技术及相关应用得到了国内外越来越多的关注,研发和应用推广的发展势头迅猛。本期《问底中国IT技术演变》,我们有幸采访到阿里云智能容器平台负责人丁宇(花名叔同),就云计算时代容器技术的发展路径,以及阿...
-
下一篇
Tablestore入门手册-数据管理-GetRow
GetRow接口概述 GetRow接口用于读取一行数据,是Tablestore最基础的API之一。官方提供了Java、Go、Node.js、Python、PHP、C#、C++ SDK。本文以Java代码为例,对GetRow接口进行详细说明。 基本使用说明 参数说明 参数名称 是否必填 参数说明 PrimaryKey 是 主键,所有主键都需要填写 ColumnsToGet 否 需要读取的列的集合,若不设置则读取所有列 MaxVersions MaxVersions 与 TimeRange 至少设置一个 最多读取多少个版本 TimeRange MaxVersions 与 TimeRange 至少设置一个 要读取的版本范围 Filter 否 过滤器,在服务端对读取结果进行过滤 Java代码示例 public void getRow() { //构造主键,主键列必须全部指定 PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); primaryKeyBuilder.addPrimary...
相关文章
文章评论
共有0条评论来说两句吧...