• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between synchronizing on static var when u implement Runnable or extend Thread

 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyExtend extends Thread{
public static Integer count;
public void run()
{
synchronized(count)
{
System.out.println("Inside synchronized");
while(true)
;
}
}
}




class MyImplement implements Runnable{
public static Integer count;
public void run()
{
synchronized(count)
{
System.out.println("Inside synchronized");
while(true)
;
}
}
}




Will there be any difference in the output of these run methods when i start four threads

//for MyExtend

new MyExtend().start();
new MyExtend().start();
new MyExtend().start();
new MyExtend().start();

or
//for MyImplement

new Thread(new MyImplement()).start();
new Thread(new MyImplement()).start();
new Thread(new MyImplement()).start();
new Thread(new MyImplement()).start();
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this compile?

You need to sync on an object not a primitive.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paritosh, welcome to the ranch!

Well, count is an Integer, not an int, so it's an object. But it's going to be null unless you initialize it. Ignoring that, these ought to run the same. As written, the first thread to get into the method will run forever but I'll guess that was just a made-up example.

Synchronizing on a static variable means all instances of the object will go through this method single file. That may be exactly what you want, but I don't think I've ever done that.

For future reference, try the "CODE" button below the post editor, and write your code between the tags that it generates. That will preserve your indenting and make things easier to follow.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks for answering.

The problem is that when I directly extend Thread(like MyExtend class) and synchronize on its static variable count,all four threads enter the synchronized block("this is the problem").

However,when I implement Runnable(like MyImplement class) and synchronize on it static variable count,only one thread is able to enter the synchronized block(which is the expected behaviour).

Please notice that there is an infinite while loop inside synchronized block.

So,if I am wrong or if its working fine on your machines,please let me know.I hope the question is clear now.

Cheers
Paritosh Ranjan
 
Gerard Jesudoss
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The thread behaviour is same whether you extend Thread or implement Runnable.
I just tried extending Thread and the beahviour is as expected. (ie, only one(first ) thread enters syncorozined block.

BTW, I have tried this on Solaris.
Tell me more about your env(like platform Java version) etc.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is common for stated behaviours to come up later as not working as advertised. There should be no difference...but you report that you *are* getting different behaviours. There might be something to look into, but there is not supposed to be any difference.

What is your jdk version and underlying os ? Is it a single processor machine ? A large campus-like network with a remote mainframe ? Classroom environment ? Sun reference implementation of the jdk or a commercial third party vendor .....

It's like Stan James says: Synchronizing on a static variable means all instances of the object will go through this method single file.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

Thank you for your time to think upon this problem.

I don't know how but when I wrote the same thing again and executed it,it is running perfectly fine.

I am using eclipse and when I posted the question I was really facing the problem mentioned.That problem shook my concepts thats why i posted the question.

Anyway its running fine now but if you guys believe,there was a problem of that type.

Cheers
Paritosh Ranjan
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by paritosh ranjan:
.... Anyway its running fine now but if you guys believe,there was a problem of that type.....


I believe you, most definitely. Beware -> OO is an illusory opening in a toroidal space-time space-time. There is no escape.
[ September 01, 2007: Message edited by: Nicholas Jordan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic