• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
class A8 implements Runnable{
static int i,j;
public void run(){
for( ; ; )
synchronized(this){
i++;
j++;
}
}
public static void main(String args[]){
new Thread(new A8()).start();
for( ; ; )
System.out.println(i+" "+j);
}
}
Can anybody explain what does synchronized(this) means here.
and how will a for loop like for( ; works.
Thank you,
Madhuri.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
synchronized(this) is a java keyword that performs a kind of locking on an object (for your example, it's the 'this' object, I.E. itself). It makes it so that ONLY one thread at a time can access a given object. It's like being put in line at the bank with only one teller. You have to wait until the person (thread) in front of you finishes before you can interact with the teller (object).
The for loop you mention is perfectly legal and is pretty much the equivalent of looping until a 'break' statement is found. In your case that would be forever or until the program terminates.
Hope this helps.
[This message has been edited by John Bateman (edited March 19, 2001).]
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u john .
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
Thanks for your answer John.
Could someone else Please expand on this issue.
Thanks in advance.
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zahid,
I am putting down the code here again. Could you please specify what doubt you have about the program. Basically synchronization can be undertaken either at the method level by specifying.
public void synchronized abc(){} // instance methods
public static void synchronized abc(){} // static methods
But the above line will make the Object in which this method exist synchronized. However suppose you have a ArrayList which you want to be synchronized. Then you have to use the second type like this
synchronized(myarraylist){
}
This will accquire a lock on the myarraylist object before it undertakes execution of the code in the block. Note that the mehtod in which this exist my itself may not be synchronized like this
public void abc(){
synchronized(myarraylist){
}
}
If you have any specific doubt please put it up.

Check out some good discussions on Threads here
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rahul,
I compiled the code and it is going in an iinfinite loop though igave a break statement.
class A8 implements Runnable{
static int i,j;
public void run(){
for( ; ; )
synchronized(this){
i++;
j++;
}
}
public static void main(String args[]){
new Thread(new A8()).start();
for( ; ; )
if(i == 10) { break;}
System.out.println(i+" "+j);
}
}
Also can I give newThread (ie instance) in my print statement directly and print xand y values .if so how to give that.
Can anybody explain. Urgent I have test next week.
Thanks in bunches
Madhuri.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhuri,
This is a pretty good one. I will try to answer it to the best of my knowledge, hoping that people will correct me if I am wrong. Every thread maintains a copy of variables that it works with. Now, everytime a lock is acquired/released, the JVM/Monitor ensures that it refreshes/updates the variables between the process and thread space.
I believe that while the i variable was incremented, the value of 10 must have crossed before the lock got released, now since your code doesn't check for value of i > 10 obviously it hangs(This goes in line with java spec's statement that thread policy should never be fully assumed while coding in Java. Various implementations have their own ways of doing it).
The first step to correct this would be to ensure that you check for i >10. Even after this you notice that the program doesn't exit as the JVM doesn't exit till all the threads (except daemon threads) have finished. So to make sure that your program exits, you either need to put another check in the loop of run() method or make the thread a
daemon thread.
Hey guys, correct me if I am wrong..
Regards, :-)
Ramesh gutty
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The original code is a real bad example for you to learn Thread and synchronization. Real, real bad!
I just point a few bad points:

  • i and j are static (class) variables, the code lock the instance to syschronize the code. Meaningless synchronization! BAD!
  • run() method is selfish, never gives other Thread a chance to run. It should have some sleep(), wait() mechanism in the infinite loop. BAD!
  • Checking i==200, stupid! It should Check i>=200. BAD!

  • If you can find some more, please do!

    Thanks!
    Roseanne
    Join our Study Group when certified
    [This message has been edited by Roseanne Zhang (edited May 13, 2001).]
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic