Results 1 to 6 of 6
  1. #1
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Why use Thread ?

    I'm reading a book about programming Applet. And in this book, they often use thread. I just know, when use thread, if I have a multicore CPU, new thread and applet will run on different core.
    But after all, I don't know the really mean of thread. It means I think my program will run sam as with no thread. For example. this is my psuedocode:
    First: use Thread.
    Java Code:
    Thread t;
    init(){
       t = new Thread();
       t.start();
    }
    public void run(){
       //do something here;
    }
    Second: does not use Thread.
    Java Code:
    init(){
        myProgram();
    }
    public void myProgram(){
       //do something here;
       //same as run() method above
    }
    Who can tell me what a difference, please.
    thanks :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Why use Thread ?

    Having more than one thread in a program allows one thread to wait for an event while another thread does some processing. For example the Swing EDT thread waits for user input while another thread in the program can be reading data from a file while another thread waits for input from an internet connection.

    In you code sample, the first one could continue executing and perhaps wait for user input after the start call on line 4.
    In the second sample the code only does one thing.

  3. #3
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Re: Why use Thread ?

    Oh. Thanks so much. I have thought more carefully about your post and see that use Thread is a must to handle user input. (keyboard, mouse...).
    For example:

    Java Code:
    Thread t;
    public void init(){
        addKeyListener();
        t = new Thread();
        t.start();
    }
    
    public void run(){
         //Do something
         //If user press some key --> Do something others.
    }
    
    public void keyTyped (KeyEvent  k){}
    public void keyPressed (KeyEvent k){}
    public void keyRealeased (KeyEvent k){}
    For above example: run() will run on thread 2. and KeyEvent will run on thread 1 same with Init(). run() and KeyEvent will run together and run() will always see what KeyEvent on thread 1 does.
    Does my assumption about above code true ? Please help me to correct.
    thanks :)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Why use Thread ?

    KeyEvent will run on thread 1
    KeyEvent is not executable code. It is an object that is passed to a key listener method.
    The key listener methods are executable code. They will be called on the GUI manager's thread (the EDT)

    The run() method looks like it will be executing on its own thread.
    The init() method could be running on another thread.

  5. #5
    PriyankMehta is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default Re: Why use Thread ?

    Threads are used for creating two or more seperate processes.
    When a thread is created, a seperate memory block/process is created for that particular function

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Why use Thread ?

    Quote Originally Posted by PriyankMehta View Post
    Threads are used for creating two or more seperate processes.
    When a thread is created, a seperate memory block/process is created for that particular function
    Thank you for posting that gem of information. Since Java doesn't have functions, You're probably referring to some other programming language that does.

    Closing this thread. The question was asked nearly 3 months ago. In future, don't add your 2c to long dead threads.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Main Thread not waiting for grand child thread to finish
    By prashanthn in forum Threads and Synchronization
    Replies: 3
    Last Post: 06-07-2011, 09:26 AM
  2. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  3. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 02:13 AM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •