• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java threads- portability  RSS feed

 
Vinod James
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are java threads completely portable?
Are there any precautions to be taken when writing threaded programs so that it is portable across platforms?
Vinod james
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Java threads are completely portable. One of the major features of the Java platform is that it is platform-independent: Java programs run on any operating system for which there is a Java runtime environment available, without the need to port or modify the program. Ofcourse it's possible to write programs in Java that only work on a specific OS, for example if you use Windows-style paths (C:\Program Files\...) it isn't going to work on Mac OS X or Linux, but major features such as threads are completely compatible.

Beware however that the underlying implementation of threads might be different on different operating systems; thread scheduling might work differently. But if your program is correct, it shouldn't rely on thread scheduling specifics of the underlying operating system anyway.
 
Tim Moores
Saloon Keeper
Posts: 4019
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread scheduling is up to the JVM, so any assumption that one thread will run before some other thread (which was possibly started later) is dangerous. Relying on thread priorities is also bound to be platform-specific (and is something you should stay away from no matter whether you're concerned with cross-platform compatibility or not).
 
Vinod James
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick replies Jesper and Tim.
How can one write a Threaded program without priorities?

If my application has like 6-7 threads, I will definitely have some threads which need to be work horses and doing more work than other threads.
How can I rewrite my application so that I am not dependent on the scheduler?

In this case then it looks like a trade off between portability and priority. Is that correct?
 
Tim Moores
Saloon Keeper
Posts: 4019
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, each thread has a priority, but what I meant was: your code should not set thread priorities, and it should not rely on higher-priority threads to get more CPU time than lower-priority threads.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod James wrote:Thanks for the quick replies Jesper and Tim.
How can one write a Threaded program without priorities?

If my application has like 6-7 threads, I will definitely have some threads which need to be work horses and doing more work than other threads.
How can I rewrite my application so that I am not dependent on the scheduler?

In this case then it looks like a trade off between portability and priority. Is that correct?



In my opinion, it is perfectly fine to use thread priorities (if you need to). You just can't rely that the scheduler will schedule it the same way on every OS platform. For example...

* With some OSes, there are less thread priority levels, so some adjacent priorites for the Java application may map to the same priority for the OS.

* With some OSes, a higher priority runnable thread is guaranteed to be running over a lower priority runnable thread -- even to the point of starving the lower priority runnable thread. With others, that have protections against thread starvation, the only guarantee is that the higher priority runnable thread will get more CPU cycles than the lower priority thread.

So... no strong guarantees or fine grain control, but should be fine for most intents and purposes.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!