Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use threads in java

 
sampath surineni
Greenhorn
Posts: 29
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Recently I came across code of a desktop alarm. Actually I'm studying the way it is coded. In that the coder has implemented thread for playing sound using sampled package. So I have this doubt that when do we use threads in java. I'm pasting the code here. Thanks in advance.

 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be careful when posting code that isn't yours as you don't want to accidentally infringe someones copyright. Also it's generally considered good practice to quote the source of the code.

In this case you generally don't want the application to freeze whilst a sound is being played so you get a background thread to play the sound.

There are numerous reasons to use multiple threads such as allowing the application GUI to remain responsive whilst a long running task is executed, to run repeating tasks at timed intervals, to listen/wait for external triggers, to speed up execution (ie by splitting a complex task across multiple processors) etc etc.
 
sampath surineni
Greenhorn
Posts: 29
Android Chrome Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your advice. I will take care next time
 
Campbell Ritchie
Sheriff
Pie
Posts: 49391
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the Java tutorials. Get (or borrow) a copy of Java Concurrency in Practice by Brian Goetz et al.
Those sources, and many others, will probably help to work out the answer to your question.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15358
39
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sampath surineni wrote:

Note that 524288 bytes is not 128 KB, but 512 KB.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic