This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

thread problem . want to break free?  RSS feed

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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****/
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
so the thread does the work faster when compared to methods?


thanks ganesh
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks ulf

great explanation nice answer

i got it

regards,
prasanna.s.k
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Gabriel Claramunt
Ranch Hand
Posts: 375
Monad Scala
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!