Results 1 to 4 of 4

Thread: run method of MultiThreading

  1. #1
    Join Date
    May 2008
    Posts
    69

    run method of MultiThreading

    Hi Guys,

    I am studying JAVA programing . I am little-bit confused about the multithreading concept.

    I am aware that Calling run() method without calling start() method can successfully execute the run() method from the current thread.

    But, Is it mandatory in multithreading to create run() method for each thread in multiple threads program?

    Please suggest me to solve this query.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: run method of MultiThreading

    Hi,

    According to my java knowledge, You will have to implement a run() method only once. And it is not mandatory to create run method for every thread while creating multithreding.

    You are able to create as many threads to run that run() method concurrently.

    I hope this will help you

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: run method of MultiThreading

    Multithreading in java is implemented by implementing Runnable interface or by using Thread class .

    Below example may solve you problem:

    Suppose, If you have a class ABC, in this situation for each instantiation of the ABC class you don't need a different implementation for this class .And same thing is happens with the run() method of multithreading.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: run method of MultiThreading

    You may use one or more than on run method in the multithreading program. See the example example:


    class Thread_demo3 extends Thread
    {
    public void run()
    {
    for(int i=1;i<=5;i++)
    {
    try{
    Thread.sleep(500);
    System.out.println("From Thread 1:"+i);

    }catch(InterruptedException i3){System.out.println("HH");}
    }
    }
    }
    class Thread_demo4 extends Thread
    {
    public void run()
    {
    for(int i=1;i<=5;i++)
    {
    try
    {
    Thread.sleep(500);
    System.out.println("From Thread 2:"+i);
    }catch(InterruptedException i2){System.out.println("XX");}


    }
    }
    }
    class UseJoin
    {
    public static void main(String ss[])
    {
    try{
    Thread t1=new Thread(new Thread_demo3());
    Thread t3=new Thread(new Thread_demo3());
    Thread t2=new Thread(new Thread_demo4());
    t1.start();
    t1.join();
    t2.start();
    t2.join();
    t3.start();
    t3.join();
    for(int i=1;i<=5;i++)
    {
    try
    {
    Thread.sleep(500);
    System.out.println("From Main:"+i);
    }catch(InterruptedException i2){System.out.println("XX");}


    }
    }catch(InterruptedException i2){System.out.println("XX");}
    }
    }

Similar Threads

  1. Multithreading in intel IPP
    By Assasin boy in forum Motherboard Processor & RAM
    Replies: 9
    Last Post: 26-09-2010, 03:25 AM
  2. Multithreading and ArrayList
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 05:16 AM
  3. How to make Multithreading games ?
    By Alek in forum Software Development
    Replies: 4
    Last Post: 15-04-2009, 09:35 AM
  4. Problem with Multithreading in C Language
    By Jagdish Gada in forum Software Development
    Replies: 4
    Last Post: 09-03-2009, 02:10 PM
  5. forks and multithreading
    By invincibledj20 in forum Software Development
    Replies: 0
    Last Post: 12-11-2008, 07:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,873,044.76240 seconds with 16 queries