为了账号安全,请及时绑定邮箱和手机立即绑定

想请问一下,什么是原生方法(native methods)?可以举个列子吗?

想请问一下,什么是原生方法(native methods)?可以举个列子吗?

莫回无 2019-03-13 18:15:58
在网上自己搜了一下,原生方法是依赖于平台的方法,同时不是以java语言实现,而是以更低级的语言,如C或C++,同时说在Class 类中定义的大部分方法均为原生方法,可以举个Class 类中的例子吗?
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

jdk源码中有很多native方法,随手复制几个出来吧:

public static native Thread currentThread();


    /**

     * A hint to the scheduler that the current thread is willing to yield

     * its current use of a processor. The scheduler is free to ignore this

     * hint.

     *

     * <p> Yield is a heuristic attempt to improve relative progression

     * between threads that would otherwise over-utilise a CPU. Its use

     * should be combined with detailed profiling and benchmarking to

     * ensure that it actually has the desired effect.

     *

     * <p> It is rarely appropriate to use this method. It may be useful

     * for debugging or testing purposes, where it may help to reproduce

     * bugs due to race conditions. It may also be useful when designing

     * concurrency control constructs such as the ones in the

     * {@link java.util.concurrent.locks} package.

     */

    public static native void yield();


    /**

     * Causes the currently executing thread to sleep (temporarily cease

     * execution) for the specified number of milliseconds, subject to

     * the precision and accuracy of system timers and schedulers. The thread

     * does not lose ownership of any monitors.

     *

     * @param  millis

     *         the length of time to sleep in milliseconds

     *

     * @throws  IllegalArgumentException

     *          if the value of {@code millis} is negative

     *

     * @throws  InterruptedException

     *          if any thread has interrupted the current thread. The

     *          <i>interrupted status</i> of the current thread is

     *          cleared when this exception is thrown.

     */

    public static native void sleep(long millis) throws InterruptedException;


查看完整回答
反对 回复 2019-04-24
  • 1 回答
  • 0 关注
  • 870 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信