Results 1 to 6 of 6
Thread: Why use Thread ?
- 02-01-2012, 06:40 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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; }
Java Code:init(){ myProgram(); } public void myProgram(){ //do something here; //same as run() method above }
thanks :)
- 02-01-2012, 08:13 PM #2
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.
- 02-02-2012, 05:20 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 27
- Rep Power
- 0
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){}
Does my assumption about above code true ? Please help me to correct.
thanks :)
- 02-02-2012, 05:27 PM #4
Re: Why use Thread ?
KeyEvent will run on thread 1
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.
- 04-29-2012, 03:46 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
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
- 04-29-2012, 04:08 PM #6
Re: Why use Thread ?
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 CLOSEDIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Main Thread not waiting for grand child thread to finish
By prashanthn in forum Threads and SynchronizationReplies: 3Last Post: 06-07-2011, 10:26 AM -
how to reduce the thread sleep time and wake up the thread
By baktha.thalapathy in forum Threads and SynchronizationReplies: 2Last Post: 06-24-2010, 08:36 PM -
Trigger main thread method from secondary thread?
By DigitalMan in forum Threads and SynchronizationReplies: 8Last Post: 01-26-2010, 03:13 AM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 08:02 AM
Bookmarks