aspose file tools*
The moose likes Threads and Synchronization and the fly likes Why the ouputs of the same programme differ ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why the ouputs of the same programme differ ?" Watch "Why the ouputs of the same programme differ ?" New topic
Author

Why the ouputs of the same programme differ ?

Tracy Tse
Greenhorn

Joined: May 24, 2010
Posts: 22
Hi,all.
Here is my whole code.

when i ran the programme using Eclipse IDE , the output is below which is beyond my expectation

trylock()true
tryLock(2, TimeUnit.SECONDS):true
trylock()true
tryLock(2, TimeUnit.SECONDS):true
acquired


nervertheless, i ran it in debug mode ,the output is below which is expected
trylock()true
tryLock(2, TimeUnit.SECONDS):true
acquired
trylock()false
tryLock(2, TimeUnit.SECONDS):false


Can any body explain why this ?
Thanks in advance !


Whatever it takes is what i have got !
Lift is the best sitcom!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Welcome to the Ranch

Please avoid coloured text; many people find it difficult to read.
It would appear that the Lock is available within the time you test it for (as here). When I tried it, copying and pasting your code, this is the output I obtained, minus some of my personal ID information:
campbell@*******-laptop:~/java$ java concurrency/Attemptlocking
trylock()true
tryLock(2, TimeUnit.SECONDS):true
trylock()true
tryLock(2, TimeUnit.SECONDS):true
acquired
campbell@*******-laptop:~/java$
Doesn't the yield() call allow the other threads to acquire and lose the locks first?

Anyway, this is a difficult question and we like to reserve "beginning" for easy questions, so I shall move this discussion. Not sure whether it is a "concurrency" problem or an "IDEs" problem, but I shall try "concurrency" and maybe the discussion will move to IDEs if that appears to be the real problem.
Tracy Tse
Greenhorn

Joined: May 24, 2010
Posts: 22
Campbell Ritchie wrote:Welcome to the Ranch

Please avoid coloured text; many people find it difficult to read.
It would appear that the Lock is available within the time you test it for (as here). When I tried it, copying and pasting your code, this is the output I obtained, minus some of my personal ID information:
campbell@*******-laptop:~/java$ java concurrency/Attemptlocking
trylock()true
tryLock(2, TimeUnit.SECONDS):true
trylock()true
tryLock(2, TimeUnit.SECONDS):true
acquired
campbell@*******-laptop:~/java$
Doesn't the yield() call allow the other threads to acquire and lose the locks first?

Anyway, this is a difficult question and we like to reserve "beginning" for easy questions, so I shall move this discussion. Not sure whether it is a "concurrency" problem or an "IDEs" problem, but I shall try "concurrency" and maybe the discussion will move to IDEs if that appears to be the real problem.

Thanks for the suggestions and explanations to my topic .
For my problem , it seems like the hint method yield() call did not actually work in time. Am i right ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Don't know. Sorry. I can only report what I found.

By the way: Please use the URL button and the entry dialogues rather than editing [url] tags by hand; it makes for neater and easier links to read. I have edited your link and I think it looks a lot better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
If anybody else has any suggestions, please fire away.

It may be that in debug mode, you are waiting more than 2 seconds before pushing the keys to proceed (f5 f6 and f7 if I remember correctly in Eclipse), so the waiting period is completed before you get to the timed() and untimed() calls.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why the ouputs of the same programme differ ?
 
Similar Threads
Question about working of ReentrantLock
Condition Variable await method
making multiple threads sleep and waking them up
Question about working of ReentrantLock
using ReentrantLock to model a Semaphore