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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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(); } } |
| |||
| 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 |
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |