2

Why the these methods

 java.lang.Thread.currentThread(),
 java.lang.Thread.sleep(),

 java.lang.Object.notify(),

 java.lang.Object.wait()

 of Thread & Object class are not implemented in java API's itself?

Why there was need to make then native method,which are parts of C/C++ programming language?

Is there any region behind that ,or java not able to give good performance?

kTiwari
  • 1,488
  • 1
  • 14
  • 21
  • 2
    Ahem... native methods are NOT part of "C/C++ programming language". Though they are implemented in C (just as the rest of JVM). – Vladimir Dyuzhev Aug 28 '12 at 18:34
  • native methods are implemented in C.means as I call Thread.sleep(). the code written on C will get executed means now the C language will interact with the OS to sleep the thread – kTiwari Aug 28 '12 at 18:38
  • The answer is 'because'. JVM implementors do what they care to do. – bmargulies Aug 28 '12 at 18:39
  • atleast i was not expecting down vote for this question who soever done this – kTiwari Aug 28 '12 at 18:41
  • The JVM can be implemented in language. – Steve Kuo Aug 28 '12 at 18:41
  • C language doesn't interact with OS. C language is just a language. A compiled binary (which happen to be written in C language) interacts with OS. That binary is called java/java.exe. – Vladimir Dyuzhev Aug 28 '12 at 18:44
  • @VladimirDyuzhev: this is my point if java.exe can interact with OS.then why these methods has to be Native why not to be simple method like anyother method in java,that can be called by java.exe to sleep a threa – kTiwari Aug 28 '12 at 18:48

3 Answers3

9

How would you write code in Java that causes the current Thread to sleep? At some point, you need to delegate to native code since it is the OS that provides and controls threads, not Java.

It might help you to understand that at a certain level, the JVM itself is written in native code, so it can talk to each OS that it runs on in OS- and machine-specific ways.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • 2
    (Point of Flippancy: by calling `Object.wait`?) – Tom Hawtin - tackline Aug 28 '12 at 18:32
  • Can you cause a thread to be suspended for about a given amount of time with just `wait()`? Obviously the point is still the same as `wait()` must then be implemented in native code. – matt b Aug 28 '12 at 18:33
  • that mean you r saying that java in not able to interact with OS...where as C/c++ can do. – kTiwari Aug 28 '12 at 18:34
  • @krishnaChandra what I am saying is that at a certain level, Java needs to interact with the OS kernel by invoking system calls, which you cannot do from pure Java code itself. It must be native code so it can be done in platform-specific ways. – matt b Aug 28 '12 at 18:35
  • Strictly speaking, may not be completely true: a JVM implementation is possible where N "hardware" threads are running M soft "JVM" threads. Then a soft thread can put itself to sleep by calling a pure-Java in-process scheduler. :) – Vladimir Dyuzhev Aug 28 '12 at 18:38
  • @krishnaChandra Java doesn't "interact with OS". JVM does. JVM is written in C (could be any language supporting given system API/ABI). – Vladimir Dyuzhev Aug 28 '12 at 18:41
  • @VladimirDyuzhev now I'm curious. Are there any JVMs known to implement Threads as soft, non-OS threads? – matt b Aug 28 '12 at 18:42
  • @VladimirDyuzhev: ya trying to say JVM. but c/c++ can interact is it? – kTiwari Aug 28 '12 at 18:44
  • 1
    @matt b None that I know now. Early JVMs (1.1) were using green threads, if I remember right. – Vladimir Dyuzhev Aug 28 '12 at 18:46
  • @krishnaChandra Sorry man, I cannot grasp what are you trying to ask. Can C++ interact? Can Canada dance? Couldn't possibly know. – Vladimir Dyuzhev Aug 28 '12 at 18:47
  • @VladimirDyuzhev: my point is simple I mean to say that the code written in C intended to interact with OS can easily interact instead the same code written in java langa...will have trouble – kTiwari Aug 28 '12 at 18:52
  • I'm afraid you are not completely understand how JVM and java code are interacting. Java code is not executed. JVM is. JVM "reads" Java code and does appropriate (native) operations. Comments space is not enough to explain it clarly, so I suggest to read something introductory on JVM. – Vladimir Dyuzhev Aug 28 '12 at 18:55
  • @krishnaChandra For Windows, OS is also behind an API which is programmable only by C or C++. If this is the thing you refer by `interaction`... – mostar Aug 28 '12 at 19:09
0

What do you mean by Not implemented in Java API's itself......

Its very much part of Java API.... I think you didn't understand what API means.....

Now in case of Threads, it depends on the OS on which the Java program is running... Threads are handled by Different OS in different ways... For example sleep() has different time implementation in Window and Linux....

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Adding to a language is much harder than adding to a library. You can add method to a class easily but adding a keyword is much harder. For this reason Java attempts to only add to the language the absolute minimum and anything which can be done in a library is done that way.

Another reason for using a library rather than adding to the language is that what seems like a good idea like using sleep(), wait() or notify() in Java 1.0 was improved/replaced in Java 5.0 by adding the concurrency library so you wouldn't use those methods these days and it would be something of a waste to make elements in the language which you wouldn't use any more.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130