| |||||||||||
Threads - what are they for? Posted by enquirer (enquirer), 5 November 2003 why threads are used in java ??what is its perposePosted by admin (Graham Ellis), 5 November 2003 Threads allow a Java program do several things in parallel - at the same time. Have you ever visited a bank or post office and found that there's just one cashier (teller) on duty, and had to wait in a queue while one customer with a complex transaction is dealt with? The same situation can arise within a Java program - especially one that's server based - and running another thread is rather like putting another teller on duty. If you have a single processor computer, you may ask "what's the point", but there can be plenty of point. Only rarely is an thread cpu bound - there will be many times where a thread is i/o bound, and during those times other threads can be making use of the cpu - thus improving the throughput and efficiency for everyone. Of course, a computer system doesn't have the physical restrictions of a bank, and it's common practise to put a new teller on duty for each customer enquiry, and have the teller go off duty when the enquiry is completed. It's also common practise to have a series of specialist threads, each of which performs one part of every transaction before passing on the partially completed task to the next specialist. The Java threads API looks very straightforward - the number of methods involved isn't huge. But beware - hidden under that simple API is a lot of power, and you need to understand that power in order to avoid doing yourself serious damage when you unleash it. Subjects you need to study include how to avoid two threads making conflicting access to the same data at the same time, and how to bring your threads back together again when multiple tasks that can be performed in parallel have been completed, and a single thread is needed to tie up the results. Have a look at subjects such as syncronisation, notify and wait. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |