• 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

Practical Usage Of Threads in java

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

We are learning lot of concepts about Threads in Java (We also see lot of Thread related questions in the SCJP exam). But how can one practically use Threads? I am working in a software company for about Three and until now I have not designed a class which extends Thread or implements Runnable.

What are all the common scenarios where the use of Thread is Mandatory? Please explain briefly. Thanks in advance
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MANDATORY? none.

But look at it this way. suppose you have an application where potentially dozens, hundreds, our thousands of users are all doing stuff. if you have a single process, then you have a few options... you can say "ok, Fred is next, i'll check on him, get his input, and then go on to the next person". if Fred goes to make dinner, all your other users are going to be waiting a long time.

However, if instead you create a thread for each user, they can all sit in an idle status, each waiting for their user to provide the input. as soon as they get it, they can then go and do their stuff, and everyone thinks they are the only ones on the system.

Granted, this is a rather contrived example, but the theory is sound. Basically, if you have to do a lot of tasks, and all might require waiting for SOMETHING to happen (user input, disk IO, access to a printer...) and each can run independant of another, threads might be a good solution.

Note that this is not specific to Java, but to any language that supports such a feature.
[ April 17, 2008: Message edited by: Fred Rosenberger ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example of a multi-threaded system is a servlet container, where a new thread is created for each request. It won't be your code creating the threads, but the server implementation creates plenty of threads behind your back.

Another example are long-running applications, like those deployed in an application server or servlet container. There's usually some cleanup work to be done every so often (say, rotating log files, or cleaning out temporary files). For those, periodically a thread running in the background would be started that does nothing but its little piece of work, and then terminates.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And multi-threading is not only important for applications running on servers; it's just as important for desktop applications with a GUI. If you start programming with Swing, you'll discover that you have to be aware of Swing's Event Dispatch Thread, and if your program is doing any processing that takes more than a second or so you should do that processing in a background thread.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it your Three is three years (?). You are correct in implying that you can spend a great deal of time writing production Java code without using threads. However one of the reasons it is good the topic is covered in the SCJP is that it is easy to write Threaded code that appears to work but then breaks in production at some indeterminate time in the future. This happened to someone where I work.

One of the reasons threading is useful is that there are situations where it can make a dramatic improvement in performance and usability. Note that the SCJP syllabus misses off some fairly important Threading topics, but they had to limit the coverage somewhere and I feel they did a good job of getting the balance right.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across such a situation once during my project where I felt that Threads is the best solution to solve it. I don't know whether I was right or not.

I had to open two pages simultaneously and the problem was that one page had to be submitted only after the other page was submitted. If in any case, first page is not submitted, say due to power failure or any other reason, second page should not be submitted.

I felt that Threading was the solution to it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic