• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java threads- portability

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic