• 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

a question in Mock2

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mock2 Question 22)
What can cause a thread to stop executing?
1) The program exits via a call to System.exit(0);
2) Another thread is given a higher priority
3) A call to the thread's stop method.
4) A call to the halt method of the Thread class?
answer: 1) 2) 3)
whether 2) is right? If in a time-sliced scheduling, it can't the answer, but in a preemptive scheduling, it is the answer. Because here does not point out the scheduling , I think 2) should not be selected. Do you think so?
with many thanks!
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I agree with you. 2 is not the correct answer
because the question must cover all platforms
a Java program can run on, not just a certain
platform.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that thread priority is dealed by JVM, therefore a higher priority thread would be picked by JVM to run first and thus lower priority thread would be halted.
I wrote a program that sets three threads at minimum priority, maximum priority, and normal priority. In the run method I have them print out stuff in loops for five times. I start the normal priority one first, then the maximum priority, and the minimum priority last. When I run it, the first output comes from the maximum, then the normal, and lastly the minimum. I thought this means that priority is taken care by JVM not by the system. I run this on a Windows XP system. And when I run two threads using same objects with out synchronizing them, the outputs shows that one thread runs all the way thru before the next runs. This seems to tell me that Windows XP home edition does not use round-robin method.
So, if priority is deal with by each system, then how could I use priority with the knowledge that in general my high priority thread would run first and halting lower priority thread?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact no Java programmer should use thread's priority to try to coordinate them. What must be used is proper synchronization. (I still think that if a programm is properly synchronized it shouldn't show time dependencies when runned on different platforms)
The point is that several OS treat priority in different ways. If all OS would ever be running the maximum priority thread, you could rely on priorities. The Java Tutorial mentions that a certain Windows OS allows high priority threads being interrupted by lower priority threads. Nice! starvation is now much more difficult, however in a given time any thread coould be running; not necessarily the highest one. Thus if a Java program desires to present results independently of the time, when executed on several platforms, it cannot rely on priorities.
There is also another source of troubles. A Java programmer cannot predict how the Java priorities has been mapped to the native priorities. That is, you may be thinking that Java priorites 0 and 2 are obviouly different ones. But maybe they have been mapped to the same one in a platform that doesn't provide ten different priorites for the user programs.
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In some cases, option (2) can cause a thread to stop executing. For example, in time-sliced thread scheduling implementations, a higher-priority thread can execute even if the lower-priority thread hasn't finished yet (actually, even lower-priority threads get a chance to be executed in this scheme.) The point is, the question asks if it can cause a thread to stop executing, not what will.
In any case, I don't think Sun's gonna ask platform-specific questions such as this one.
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic