wood burning stoves
The moose likes Beginning Java and the fly likes when to use threads in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when to use threads in java" Watch "when to use threads in java" New topic

when to use threads in java

sampath surineni

Joined: Apr 02, 2012
Posts: 29


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

Joined: Aug 07, 2007
Posts: 2832
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

Joined: Apr 02, 2012
Posts: 29

Thanks for your advice. I will take care next time
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
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

Joined: Aug 16, 2005
Posts: 15081

sampath surineni wrote:

Note that 524288 bytes is not 128 KB, but 512 KB.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link: http://aspose.com/file-tools
subject: when to use threads in java
It's not a secret anymore!