Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need explonation of this question about threads!

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need explonation of this question about threads!

    Hi to everyone, actually i wanna know why we have to use Threads. What is purpose of using threads since we can output the same result just with writting this code in main class:
    System.out.print("thread #1: ");
    for(int i=0; i<10; i++){ 
    System.out.print(i+" ");
    }
    System.out.println();
     
     
    System.out.print("thread #2: ");
    for (int i = 10; i < 20; i++) {
    System.out.print(i + " ");
    }

    ---------------------------------------------------------------------------------------------
    public class HelloRunnable implements Runnable {
     
    //run method for HelloRunnable i.e. for the thread #1
    public void run(){
    System.out.print("thread #1: ");
    for(int i=0; i<10; i++){
    System.out.print(i+" ");
    }
    System.out.println();
    }
    public class HelloRunnable2 implements Runnable {
     
    // run method for HelloRunnable2 i.e. for the thread #2
    public void run() {
    System.out.print("thread #2: ");
    for (int i = 10; i < 20; i++) {
    System.out.print(i + " ");
    }
     
    }
     
    }
     
     
     
    public static void main(String[] args) {
     
    (new Thread(new HelloRunnable())).start();
    (new Thread(new HelloRunnable2())).start();
     
    }
     
    }



    what is actually the difference doing this with thread and doing without threads since the result is the same?
    Anyone who can explain it ?
    Last edited by helloworld922; January 24th, 2011 at 07:32 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need explonation of this question about threads!

    Quote Originally Posted by lulzimfazlija View Post
    what is actually the difference doing this with thread and doing without threads since the result is the same?
    Anyone who can explain it ?
    Basically, this is a bad example of Threading. Instead: what happens if each Thread prints out a trillion numbers? What happens if you put a Thread.sleep(100) after printing out each number?

    I wouldn't really expect you to get the same output each time, even with your small example. I recommend providing an SSCCE that we can run that demonstrates what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need explonation of this question about threads!

    ...actually i wanna know why we have to use Threads. What is purpose of using threads...
    Agreed, a bit of a bad example. Threads allow you to perform work in parallel. Doing so allows you to essentially do 2+ things at once (actually its a bit more complex than that but from a layman's standpoint that's what they do). This can reduce the time it takes to complete a task (can divide the work up), prevent locking up a GUI, or perform tasks which need to be done seemingly the same time

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need explonation of this question about threads!

    So is there any practical example that anyone can show us or just theory , bc im alredy tired from theory.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need explonation of this question about threads!

    I gave you a few ways to make your example more practical, and the Concurrency tutorials contain numerous examples: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Need explonation of this question about threads!

    Quote Originally Posted by lulzimfazlija View Post
    So is there any practical example that anyone can show us or just theory , bc im alredy tired from theory.
    One common example is playing music (particularly in games).
    If you were only using one thread to do this, then the game wouldn't be able to continue until the music has finished playing. However, if you use two threads then you can update the game in one thread while the other one is playing the music.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need explonation of this question about threads!

    Anything which can be done asynchronously (at the same time, independent of each other) can benefit from multi-threading.

    In your example, this isn't the case (I'm assuming you want the numbers printed out in order) because all the numbers in HelloRunnable have to be printed out before you want the numbers in HelloRunnable2 to be printed out, which is a synchronous operation(HelloRunnable2 must wait for HelloRunnable to finish to get the correct results).

    There are many examples of things which can be run asynchronously. Generally, they fall into these two broad categories:

    1. You're looking to speed up something which is computationally expensive and can be split up into asynchronous tasks, either partly or in whole. Note that you'll only get any appreciable speed-up if you actually have multiple cores on your computer.

    2. You're looking to improve the response of different asynchronous components (such as a GUI interface which can still receive user input while data processing happens on another thread to prevent either side from hanging). This can be beneficial on single core systems as well as multi core systems because the OS can switch between the tasks automatically for you, therefore if you're computation takes a long time, some CPU clocks are still being diverted to your GUI control allowing the user to still use it at the same time.

Similar Threads

  1. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 05:33 AM
  2. Servlet Threads Question
    By doNotPost in forum Threads
    Replies: 1
    Last Post: October 21st, 2010, 09:19 AM
  3. How Can I run java threads
    By Saeid in forum Threads
    Replies: 12
    Last Post: August 6th, 2010, 02:12 PM
  4. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  5. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM