Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Threads - what are they for?

Posted by enquirer (enquirer), 5 November 2003
why threads are used in java ??what is its perpose

Posted 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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho