Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Print Even odd numbers using thread interrupt

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
    I am brushing my thread knowledge.I tried to print even odd numbers using interrupt() method.I took reference from internet and wrote the below code--
<br /> <br /> Question 1> Why Thread.sleep(1000L) is required at line 15?If we don't mention this then in the execution I can see only Even Thread print 20, nothing else.Why this is happening?
Question 2> After completion of printing 1 to 20 properly how I should stop the execution of the program?

Thanks

 
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you do need Thread#sleep. The notion of using Thread.sleep(Integer.MAX_VALUE) in an infinite loop makes me go all sorts of funny colours. I don't know where to start about it.
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed Thread.sleep(Integer.MAX_VALUE); and related interrupt call.It prints fine.But the same questions remain same--

Question 1> Why Thread.sleep(1000L) is required at line 13?If we don't mention this then in the execution I can see only Even Thread print 20 in eclipse output, nothing else.Why this is happening?
Question 2> After completion of printing 1 to 20 properly how I should stop the execution of the program?



 
Ranch Hand
Posts: 218
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this because how parallel excution works, no matter if true parallel on two physical cores or pseudo parallel on one thread-scheduled core

the main reson for your output might be that the end of the loop is passed before the othere threads fully started up so your int is already 20 when print is first executed - remeber: modern cpus run at giga-hertz - thats 1.000.000.000 operations per second - your count up to 20 is done in 20 cycles - the create and start up a new thread it take couple 100 cycles

so you have to slow down your main thread

the other question: use daemon threads or use while(flag) instead of true
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Matt for the clarification
 
please buy this thing and then I get a fat cut of the action:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic