• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the ouputs of the same programme differ ?

 
Tracy Tse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Campbell Ritchie
Sheriff
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic