1

Why isAlive() method has been declared native in Thread.java? Which class implements this? I know native methods are implemented outside Java. The class which implement this is loaded by loading the respective library. But I am curious to know which class implements isAlive() method? And why it is declared native?

ParagJ
  • 1,566
  • 10
  • 38
  • 56
  • Threads are part of the operating systems and implemented in reality by the operating system (assuming you have native threads) Thus when you want to access this underlying resource, a native call needs to be made. – Peter Lawrey Feb 04 '15 at 09:50
  • 2
    I would way it's because JVM does not implement it's own scheduler and it uses native OS threads. So this method is just a wrapper around some OS kernel syscall. – ibre5041 Feb 04 '15 at 09:51
  • Voted to reopen because this is *not an exact duplicate*, this is a *specific* question which can be answered *reasonably* . – TheLostMind Feb 04 '15 at 09:55
  • 1
    "The classes which implement these are loaded by loading the respective libraries. But I was curious to know which class implements isAlive() method?" Native methods do not need to be implemented by classes. They are implemented _natively_ - usually in C or C++. What are you actually asking? – davmac Feb 04 '15 at 09:55
  • 2
    Your question embodies not one but two contradictions in terms. If it's native, it isn't implemented by a class at all. And if it's implemented 'outside Java', it isn't implemented in a Java class. – user207421 Feb 04 '15 at 09:56
  • I don't want to override it anyway. I am just curious to know where is it's implementation. – ParagJ Feb 04 '15 at 09:58

1 Answers1

3

Why isAlive() method has been declared native in Thread.java?

Possibly because it has to be implemented in native code. Possibly because it can be implemented more simply in native code. Possibly for historical reasons; i.e. it was declared to be native a long time ago, and it has been left that way to minimize potential disruption cause by "fixing" it.

You might get more insight by looking at the C++ source code for native code methods for Thread, across all platforms. But obviously the source code of historical implementations is not available.

Which class implements this?

None. It is native.

I know native methods are implemented outside Java. The class which implement this is loaded by loading the respective library.

Obviously it is not a Java class then ...

But actually, the library is probably linked into the core java executable rather than being dynamically loaded.

But I am curious to know which class implements isAlive() method?

Ermm ... are you talking about a C++ class? It will be platform dependent. A lot of the native classes have different versions in the code base for Windows, Linux and Solaris (and possibly Mac now, I guess).

If you are really curious, I suggest you checkout the code from the OpenJDK Mercurial repos, or download the source bundles, and search. Unfortunately, the OpenJDK project doesn't provide a nice convenient web-based source code browser for the repository. (So even if I found it for you, I couldn't give you a stable link to it.)

And why it is declared native?

You are repeating yourself. (But Java classes aren't declared native. Java methods are ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Yes I was talking about a C++ class or any program which provides its implementation. But thanks anyway for good wrap up. – ParagJ Feb 04 '15 at 10:19
  • 1
    @ParagJ: download the source of openjdk and check it out – Uku Loskit Feb 04 '15 at 10:44
  • Re, "proper synchronization could be tricky". Not just tricky, impossible! The name `isAlive()` is misleading: If you call `t.isAlive()` and it returns true, that does _not_ mean that thread `t` is alive. It means that the thread _was_ alive at some instant before or during the `isAlive()` method call, but it does not guarantee that the thread will _still be_ alive by the time your caller takes some action based on the return value. – Solomon Slow Feb 04 '15 at 14:50
  • @jameslarge - I forgot about that misleading name / semantic. Clearly, there is a race between the (hypothetical) variable being set and the thread actually starting. Doing isAlive in native code probably allows them to avoid that. – Stephen C Feb 04 '15 at 23:53