JNI学习之-----Local and Global References

1.问: jni 支持多少种 references?

   答:

  • JNI supports three kinds of opaque references: local references, globalreferences, and weak global references.


2.问: Local 和 global references  的生命周期一样吗?

   答:

  • Local references areautomatically freed, whereas global and weak global references remain validuntil they are freed by the programmer.

3.问: local reference 的有效期是怎么样的? 它什么时候产生,什么时候消亡?

   答:A local reference is valid only within the dynamic context of the nativemethod that creates it, and only within that one invocation of the native method.All local references created during the execution of a native method will be freedonce the native method returns.

    

Local references are also only valid in the thread that creates them. A localreference that is created in one thread cannot be used in another thread. It is a pro-gramming error for a native method to store a local reference in a global variableand expect another thread to use the local reference.

4.问: 请你介绍一下global references的基本情况?

   答: 

You can use a global reference across multiple invocations of a native method. Aglobal reference can be used across multiple threads and remains valid until it isfreed by the programmer. Like a local reference, a global reference ensures thatthe referenced object will not be garbage collected.

5.问: 怎样去创建一个global references ? 

   答:

global ref-erences are created by just one JNI function, NewGlobalRef  例如:

   d->audio_record = jni_env->NewGlobalRef(d->audio_record);

6.问:怎样去释放Local references ?

   答:(*env)->DeleteLocalRef(env, jstr);

7.问:怎样去释放Global references?

   答: DeleteGlobalRef




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值