Java threads- portability
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Are there any precautions to be taken when writing threaded programs so that it is portable across platforms?
Vinod james
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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
Bartender
Posts: 7645
178
posted 13 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 13 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
| Do Re Mi Fa So La Tiny Ad Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







