• 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

Thread

 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys !

could any body please tell me how the following piece of code works,, i would appreciate if you could comment on each line with a little detail,, i'm a little confused

 
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hama Kamal

The Question is very good. Here total 500 thread will create and they will invoke on the same object at Line-12 and also Because the run() is synchronized so the threads can not run concurrently. and the output will always be 5 10 15 20 25............ 500.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aashu Mahajan wrote:Here total 500 thread will create

100
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you guys ,, but i would apprecaite that if you could tell me how it works in detail....

what this line is doing exactly?


2nd point after removing some parts i still get the same resutl as shwon below,,, could any one exlain why?, please?

original code



here is the code after removing those parts which i mentioned about ,,,,it gives the same output with the one above ,,,,why ??!!!





 
Greenhorn
Posts: 7
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yours changes will not have impact on program output because:
1. Following condition never will be true:

because each new thread will start with i value that can be divided by 5

2. Following code:

means that current thread should stop working for a while and check if there are any other threads with higher priority to run. So no matter if thread will be interrupted or not work must be done from beginning to the end.

So no matter if you leave or remove above parts of code your output will not change.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks buddy
 
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:thank you guys ,, but i would apprecaite that if you could tell me how it works in detail....
what this line is doing exactly?



Causes the current thread to release the resource called "CPU/processor", and allow other threads to execute.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,
to the best of my knowledge, the yield() method "tends" to make the currently running thread head back to a runnable state to allow other threads of the SAME priority get a turn. That said there is NO guarantee that the yield() method will effectively do that. Even if the yield() method does cause a thread to step out of running and back to runnable, again there is NO guarantee the yielding thread won't just be chosen again over all the others!.
@Hama Kamal the above summary explains why this line of code: did NOT "seem" to have any impact on the program(refer to pages 724, 725, and 726 of the K&B book).
For the purpose of the exams the question and option will adhere to the realities about the yield method because the creators are aware that the yield() methods behaviour -I REPEAT- is NOT guaranteed.

I hope this helps

Regards

Ikpefua
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys ,,
 
Tomski Simon
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote: the yield() method "tends" to make the currently running thread head back to a runnable state to allow other threads of the SAME priority get a turn.



I think both of us are right:) Threads of the same and higher priority will be allowed to run. Below you can find quote from "Java Thread Programming" - Paul Hyde (Chapter 6 - Voluntarily Relinquishing the Processor: Thread.yield()):

To help ensure that other threads in the VM get a turn to run on the processor, a thread can voluntarily give up its turn early. If a thread invokes the static method Thread.yield(), the thread scheduler will swap it off the processor and allow another thread to run. It is likely (but not guaranteed) that only threads having a priority equal to or greater than the one that yielded control will be considered by the thread scheduler.


 
reply
    Bookmark Topic Watch Topic
  • New Topic