• 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 confused

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

Below from Sun free assesment :

class NoGo implements Runnable {

private int i;

public synchronized void run () {

if (i%10 !=0) {i++;}

for (int x=0;x<10;x++,i++) {
if (x==4) Thread.yield();

}

System.out.println (i+" ");

}

public static void main (String[] args) {

NoGo n = new NoGo ();

for (int x=0;x<101;x++) {

new Thread (n).start();
}



}




}

When I compile above code, it gives me output value from 10 to 1010. Could somebody help me to understand how this code works, please? and thanks for your reply.

regards,
-Mila-
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Vierda.
I dont understand what are you want to do about this code. Especially the line which is 'if (x==4) Thread.yield();' . I think this is useless in this code. Del this line will not change the result.

Changing the line 'sysout' with 'System.out.println("Thread " + Thread.currentThread().getId() + ": " + i );' maybe give some help you.
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Yu Yong,

Thanks for your reply. Do you mean since line 'if (x==4) Thread.yield();' is useless so the code continue looping 9 times or until its condition meet x<10 ??

regards,
-Mila-
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your main method you are creating new threads in the for loop.when the
first thread starts running, the following operations will be performed.

Step 1:

i = 0 and (i%10 !=0) returns false, hence i++ value will not increase here.


Step 2:

now the for loop is getting executed in run method.
x = 0 and i = 0
(x == 4) returns false for first four iterations i.e from 0 to 3.
when x == 4, Thread 0 is suspended and Thread 1 or Thread 2 or Thread n will get the control depending on OS
and execute the for loop until x < 10. when x is greater than 10, control will go to SOP and prints the
value of i. i.e 10.

The above steps will continue until all your thread completes its work. In the mean time, the value of i will be
incremented for each iteration.


This is what exactly happening in your example.
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Satishkumar,

Thank you for your brief explanation.I got the point.

regards,
-Mila-
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why we can say that first thread will be the one created first because the run method is declared as synchronized...
I don't think, deleting this keyword from run method will make any difference as far as output is concerned.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic