Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

holdsLock is working strange  RSS feed

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo I have the follow problem with the holdsLock() method.For the code :

public WaitTest() {
t1 = new Thread() {
public void run() {
waitMethod();
}
};
t1.setName("Thread 1");

t2 = new Thread() {
public void run() {
waitMethod();
}
};
t2.setName("Thread 2");

t1.start();
t2.start();
}

private synchronized void test() {
System.out.println("Curent thread : " + Thread.currentThread().getName());
System.out.println(t1.getName() + " has monitor : " + t1.holdsLock(this));
System.out.println(t2.getName() + " has monitor : " + t1.holdsLock(this));
}

So Ihave two threads and each thread tries to get the monitor by entering in the method test.The method is atomic and can be executed onyl by one thread once - that means the output must be like :

Curent thread : Thread 1 -> in the method enter thread T1
Thread 1 has monitor : true -> it has the monitor,
Thread 2 has monitor : false -> thread t2 waits (it has no monitor)

but my output is :

Curent thread : Thread 1
Thread 1 has monitor : true
Thread 2 has monitor : true
Curent thread : Thread 2
Thread 1 has monitor : true
Thread 2 has monitor : true


Why ?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no code shown for waitMethod(). What does that do?

There is a simple typo in your test() method. You have a "t1" where you should have a "t2". Is this just a typo in the JavaRanch message, or is it really in your code. If the latter, that's the problem.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh sorry, my mistake the method waitMethod() is the method test().
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mihai Radulescu:

So Ihave two threads and each thread tries to get the monitor by entering in the method test.The method is atomic and can be executed onyl by one thread once - that means the output must be like :
...
Why ?


The method can only be executed by one thread _at a time_. After the first thread has finished that method it releases the lock, the second thread is then allowed to get the lock and enter. They just can't both be there at the same time. That is what synchronized means.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Gilbert but how you explain the output.

Curent thread : Thread 1
Thread 1 has monitor : true
Thread 2 has monitor : true

At least one thread must return on hasMonitor() method false and in the method can be run only one thread once (it is synchronize).But with all I still can not explain this strange output.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, can you edit your original post and include the new code without the errors?

Basically holdsLock is a static method. It should be called without the reference e.g.

[ November 16, 2004: Message edited by: CL Gilbert ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have it.I look in the java api and I found : the holdsLock() is a staitc method and it returns true if and only if the current thread holds the monitor lock on the specified object.
In the other words it works only with the current thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!