Just_another_developer
Just_another_developer

Reputation: 5957

Are Threads in Java platform dependent?

It is obvious that OS scheduling/ threading algorithms have their impact on Java threads but

can we safely say that Threads are OS/machine dependant?

If this is the case then doesn't it make Java platform dependant?

Upvotes: 7

Views: 4276

Answers (3)

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

Even on the same platform, if you write unsafe multi-thread code, behavior can depend on the full configuration details, the rest of the machine load, and a lot of luck, as well as hardware and OS. An unsafe program can work apparently correctly one day, and fail the next on the same hardware with more-or-less the same workload.

If you write safe multi-thread code, code that depends only on what is promised in the Java Language Specification and the library APIs, the choice of platform can, of course, affect performance, but not whether it works functionally.

Upvotes: 3

Joachim Sauer
Joachim Sauer

Reputation: 308011

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.

Upvotes: 8

Adam Arold
Adam Arold

Reputation: 30528

... Java will usually use native threads, but on some operating systems it uses so called "green threads", which the JVM handles itself and is executed by a single native thread.

You shouldn't have to worry about this. It is all handled by the JVM, and is invisible to the programmer. The only real difference I can think of is that on an implementation that uses green threads, there will be no performance gain from multi-threaded divide-and-conquer algorithms. However, the same lack of performance gain is true for implementations that use native threads, but run on a machine with a single core.

Excerpt from JVM & Java Threads Scheduling

Upvotes: 3

Related Questions