Java平台中的线程是否依赖?

Jus*_*per 5 java multithreading java-ee

很明显,OS调度/线程算法对Java线程有影响但是

我们可以安全地说线程是OS /机器依赖的吗?

如果是这种情况,那么它不会使Java平台依赖吗?

Joa*_*uer 7

Yes, the details of the scheduling of threads in Java depends on the JVM implementation and (usually) on the OS implementation as well.

But the specifics of that scheduling is also not specified in the Java SE specification, only a selected few ground rules are specified.

This means that as long as the OS specific scheduling is conforming to those ground rules, it is also conforming to the JVM spec.

If your code depends on scheduling specifics that are not specified in the JVM spec, then it depends on implementation details and can not be expected to work everywhere.

That's pretty much the same situation as file I/O: if you hard-code paths and use a fixed directory separator, then you're working outside the spec and can not expect your code to work cross-platform.

Edit: The JVM implementation itself (i.e. the JRE) is platform dependent, of course. It provides the layer that allows pure Java programs to not care about the platform specifics. To achieve this, the JRE has to be paltform specific.

  • @Sobia你是绝对正确的.需要为每个平台单独实现JVM,因为Java是自下而上设计的,以保护您远离平台细节.JVM就是抽象层. (3认同)
  • 是的,Sobia:Java JVM与*平台无关.但是,纯Java*程序*可以是独立于平台的(只要它们不依赖于任何实现定义的行为). (2认同)