• 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

Doubt in Thread Synchorizeation

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

In the below code.

public class testthread extends Thread{
static int i=0;

public synchronized void run(){
i++;
System.out.println("this is run()");
}


public static void main(String... args){

for(int i=0;i<5;i++){

new Thread().start();
}
System.out.println("value of i is " + i);

}
}

we have synchorized void run() means Synchronize(this) lock based on current object when the for loop executes it creates five threads and calls start() which intern calls run() since it is synchorized only one thread can access i++ and finally it will print 5 am i correct but the answer to this question was different my doubt is that doesnt Synchronize(this) block two threads in the above code.

Thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
testthread is never run, since the code allocates 5 Thread objects, not 5 testthread objects. Replace "new Thread()" with "new testthread()", and see what happens.

Even then, what this code does is not necessarilty what you may think it does. The JVM may or may not run any of the threads before the println statement of the main method is executed, so any value of i between 0 and 5 could be printed.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, actually it is new testthread() only now my question is synchronized(this) will it block the threads created in the for loop or it will not block threads created in the for loop.

Thanks
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Ulf's explanation:

Originally posted by Subu Mhathma:
[QB]since it is synchorized only one thread can access i++


Not correct, since you are synchronizing on five different objects. As result, all five threads can execute i++ at the same time.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means to say synchronized(this) will not block the threads created in the for loop can any one please explain the way "synchronized(this)" and "synchronized(class literal)" behaves and any links on this will be very help full
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first you need to change your run method as follows:

public void run()
{
synchronized(Test.class)
{
i++;
System.out.println("this is run()");
}
}

now every thread will be synchronized. but still output will be unexpected from i values 0 to 5. because you cannot assure that whether main thread will execute first or other threads.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to read the Sun Java Concurrency Tutorial , particularly the section on synchronization. It talks about using locks for synchronization.
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Not correct, since you are synchronizing on five different objects. As result, all five threads can execute i++ at the same time.



Since i is a class variable, at most one class lock is possible. So all the five threads cannot execute i++ at the same time.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abdullah Al Mamun:
Since i is a class variable

That i is static is unimportant. The important thing is, that all threads use the same object for synchronization.

at most one class lock is possible

It is possible, however it's not used in the original posting.

So all the five threads cannot execute i++ at the same time.

Conclusion: In the original posting, all five threads can execute i++ at the same time.
reply
    Bookmark Topic Watch Topic
  • New Topic