• 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

thread problem . want to break free?

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the advantage of using threads when we can do the same task using functions?

A simple class with a main function

say

class Abc {
public static void main(String args[]) {
System.out.println("hi");
}
}

/**does this program use threads***/
/***is it single thread****/
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya it is and the name of that thread is "Main" .Thread is important when their exist scenarios like AWT etc.,
But for small programs which doesnt implement the thread interface and extend the thread class it is not important to know the thread.

Hope you got it
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so the thread does the work faster when compared to methods?


thanks ganesh
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multi-threaded programs can be faster than single-threaded ones, they can also be slower - it depends on the circumstances. But in general, they are used to get more stuff done in the same time. If there aren't at least two things that could usefully be done at the same time, it makes little sense to use threads, because they do cause complications.

But note that even though the code shown above is single-threaded from the program's point of view, the JVM always has multiple other threads running in the background for various housekeeping tasks, e.g. garbage collection.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ulf

great explanation nice answer

i got it

regards,
prasanna.s.k
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prasanna sheregar:
so the thread does the work faster when compared to methods?



A thread is not an alternative to a method. All methods are actually executed in threads. You can think of every thread as being a sequence of method executions, while several threads can be executed in parallel.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corporate legend: In the late 50s my company had computers with vacuum tubes and some true giants wrote custom languages, compilers, utilities and such. Somebody wired a speaker across some circuit so you could hear the sounds different instructions made. They wrote programs with loops for different pitches to play Christmas carols. During tape IO the main processor went silent while it waited for the tape drive to spin up, read a block and send it back. One of the gurus piped up "Hey, this thing is quiet an awful lot of the time. We could try to run a second program during the IO waits". They all stared at the silent speaker for a while and said "Nahhh."

Multi threading can be faster if the program waits for something a lot. Disk IO or calls to remote systems are good candidates. It's also cool for desktop applications to do long running work on a background thread and keep the GUI thread available so you can hit the "cancel" button.
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasanna,
Maybe this will help you to understand threads: The Java Tutorial's Lesson on concurrency
[ August 02, 2007: Message edited by: Gabriel Claramunt ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic