IT Community - Software Programming, Web Development and Technical Support

How to implement Multithreading in Java?

This is a discussion on How to implement Multithreading in Java? within the Java Programming forums, part of the Software Development category; How to implement Multithreading in Java?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-25-2007, 11:25 PM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Question How to implement Multithreading in Java?

How to implement Multithreading in Java?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 11:25 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: How to implement Multithreading in Java?

To create threads, create a new class that extends the Thread class, and instantiate that class. The extending class must override the run() method and call start() method to begin execution of the thread.

Inside run(), you will define the code that constitutes a new thread. It is important to understand that run() can call other methods, use other classes and declare variables just like the main thread. The only difference is the run() establishes the entry point for another, concurrent thread of execution within your program. This will end when run() returns.

public class MyThread extends Thread {

String str1;

public MyThread(String str2){
str1 = str2;
}

public void run(){

try {

for(;
{
Thread.sleep(1000);
}

} catch(InterruptedException e) {

System.out.println("sleep intreupted");
}
}

public static void main(String[] args) {

Thread t1=new MyThread("First Thread");
Thread t2=new MyThread("Second Thread");
t1.start();
t2.start();
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-17-2007, 08:02 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Wink Re: How to implement Multithreading in Java?

Hi,
We can implement the multithreading concept in java in two ways...
1..Extending the thread class..
2..Implementing the runnable interface
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-11-2007, 06:08 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: How to implement Multithreading in Java?

Multithreading
*Executing program with multiple threads in parallel.
*Special form of multiprocessing..

Creating Threads in Java
Runnable interface
*Create object implementing Runnable interface.
*Pass it to Thread object via Thread constructor.

Example
public class MyT implements Runnable
{
public void run()
{
… // work for thread
}
}
Thread t = new Thread(new MyT()); // create thread
t.start(); // begin running thread
… // thread executing in parallel

Alternative (Not Recommended)
Directly extend Thread class

Example
public class MyT extends Thread
{
public void run()
{
… // work for thread
}
}
MyT t = new MyT(); // create thread
t.start(); // begin running thread


Why not recommended?
*Not a big problem for getting started.
-but a bad habit for industrial strength development.
*The methods of the worker class and the Thread class get all tangled up.
*Makes it hard to migrate to Thread Pools and other more efficient approaches


Java Multithreading Example

public class ThreadExample implements Runnable
{
public void run()
{
for (int i = 0; i < 3; i++)
System.out.println(i);
}
public static void main(String[] args) {
new Thread(new ThreadExample()).start();
new Thread( new ThreadExample()).start();
System.out.println("Done");
}
}

Possible outputs
0,1,2,0,1,2,Done // thread 1, thread 2, main()
0,1,2,Done,0,1,2 // thread 1, main(), thread 2
Done,0,1,2,0,1,2 // main(), thread 1, thread 2
0,0,1,1,2,Done,2 // main() & threads interleaved

Last edited by leoraja8 : 09-11-2007 at 06:39 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MultiThreading in Ruby S.Vinothkumar Ruby 8 11-20-2007 01:49 AM
How to implement threading in VB.Net? amansundar VB.NET Programming 2 10-30-2007 07:41 AM
What is Cryptography? How to implement MD5 ? H2o General Web hosting Discussions 1 07-21-2007 01:04 AM
What is multithreading? Jeyaseelansarc C and C++ Programming 0 05-17-2007 08:26 AM
How to Implement SEO to a ASP.NET Web Application. prasannavigneshr Search Engine Optimization 1 05-12-2007 03:06 AM


All times are GMT -7. The time now is 10:57 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0