• 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

Dan's Threads mock exam query

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In Dan's Threads mock exam there some points I am not clear about :

1. If execution of a block completes abruptly , then lock is released .

How does execution completes abruptly ? Is it exception thrown etc ...

2. We should not ynchronize on a method local variable , because each thread has its own instance of the object and lock ?

If all threads have a seperate instance of object and lock , then all threads can acquire lock of the object at the same time . Please clarify this , if possible with some example.

3. Yeild() is used to give chance to other thread to run . But does it means that other thread should be of same priority ? Can a thread of lower priority run on any platform , if yeild is invoked ?

4. What is the priority Daemon threads ? Is it platform specific or always MIN_PRIORITY ?

5. Also initailly does main() thread has NORMAL_PRIORITY ? Is normal priority always = 5 on all platforms ?

Thanks,
Lavjeet
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lavjeet,

I have not read Dan's Threads mock exam theory, but still trying to answer ssome of your questions.

Answer for 2nd.
What i think is, you are wrong in visiualising an object and a thread. You are wrong in saying that all threads have separate objects
Exmple is :



See, we have only one object og MyThread, and mutiple threads are running for the same. I hope if u would try to visiualise this exmple, you can get your problem done.

Answer for 3.
yeild is very simple method that does nothing but tells the compiler that i dont want to continue processing, please put me in not-running state. Process does so, and then as per its schduling algo or other settings, finds which thread to be executed. Now, this new thread can be the same thread or a thread with high prority or with low proirity.

I hope this will clear.

Regards
Sandeep Jindal
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lavjeet Khanuja:
Hi All,

3. Yeild() is used to give chance to other thread to run . But does it means that other thread should be of same priority ? Can a thread of lower priority run on any platform , if yeild is invoked ?

4. What is the priority Daemon threads ? Is it platform specific or always MIN_PRIORITY ?

5. Also initailly does main() thread has NORMAL_PRIORITY ? Is normal priority always = 5 on all platforms ?



3. A lower priority thread could run. There's no guarantees in this instance. The scheduler could schedule in a lower priority thread. For the exam, yield() doesn't have much guaranteed behavior. It certainly DOES NOT guarantee that only a higher priority thread will run next. It DOES NOT guarantee that a different thread will be the next to run.

In addition, look at this from K&B ch9:


The yield() method is not guaranteed to cause a thread to leave the running state, although if there are runnable threads of the same priority as the currently running thread, then the current thread will probably leave the running state.



4. The same priority as the thread which created the thread.

5. Yes and yes (but reasonable programming practice would suggest you use the provide constant Thread.NORM_PRIORITY. I wouldn't get hung up on numbers though.

More important is the concept of a thread that gets created inherits the priority, and it might be more useful to create threads with values of the current thread's priority +1 or the current thread's priority -1 rather than using numbers.

Java in a Nutshell, 4ed (Flanagan):
 
reply
    Bookmark Topic Watch Topic
  • New Topic