Results 1 to 6 of 6
  1. #1
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default Executing two threads simultaneously.

    Hello,

    I have two threads and I want to execute them simultaneously.

    This is my code:

    Java Code:
    public final class MainClass {
    	public static void main(String[] args){
    		Thread a = new MyThread(doa);
    		Thread b = new MyThread(dob);
    		a.run();
    		b.run();
    	}
    }
    This, however, executes Thread a first and then after Thread a is finished it executes Thread b.

    But I want to run them simultaneously. How do I do that?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Onra View Post
    Hello,

    I have two threads and I want to execute them simultaneously.

    This is my code:

    Java Code:
    public final class MainClass {
    	public static void main(String[] args){
    		Thread a = new MyThread(doa);
    		Thread b = new MyThread(dob);
    		a.run();
    		b.run();
    	}
    }
    This, however, executes Thread a first and then after Thread a is finished it executes Thread b.

    But I want to run them simultaneously. How do I do that?
    Use the start() method of the Thread, not its run() method (it's all in the API documentation).

    kind regards,

    Jos

  3. #3
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Ok, now a new question.

    How can I make two threads run alternatively?

    Thread A
    Thread B
    Thread A
    Thread B
    Thread A
    Thread B

    etc.

    I want to do it with a control variable, but it doesn't work out.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Onra View Post
    Ok, now a new question.

    How can I make two threads run alternatively?

    Thread A
    Thread B
    Thread A
    Thread B
    Thread A
    Thread B

    etc.

    I want to do it with a control variable, but it doesn't work out.

    put one thread to sleep for some millis inside your thread so that the other thread can be served.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Onra View Post
    Ok, now a new question.

    How can I make two threads run alternatively?

    Thread A
    Thread B
    Thread A
    Thread B
    Thread A
    Thread B

    etc.

    I want to do it with a control variable, but it doesn't work out.
    If you want that there's no reason to run two threads; thread B has to wait until thread A has finished and vice versa. Better use a simple loop that runs both jobs alternately.

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Onra View Post
    But I want to run them simultaneously. How do I do that?
    unless you have a mulicore-processor this is not really possible. the jvm switches between threads that fast that it looks like "simulaneously", but it isn't. and you can't force when a threads has to be started, the threads-scheduler decide when. nevertherless here is a bit of code that simulate something like you want.

    Java Code:
    public class MyThread extends Thread {
    
    	public void run() {
    		while (true) {
    			try {
    				System.out.println("Hallo, I'm thread " + this.getName());
    				this.sleep(5);
    			} catch (InterruptedException ex) {
    				ex.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		MyThread a = new MyThread();
    		MyThread b = new MyThread();
    		a.start();
    		b.start();
    	}
    
    }

    the principle is simple. after a thread has prompted "Hallo, I'm ..." it sleeps for 5 milliseconds so that the other thread can be started. watch out: the example has a endless while-loop.
    Last edited by j2me64; 04-05-2010 at 08:06 PM.

Similar Threads

  1. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 01:53 PM
  2. Multiple persistance unit simultaneously, issue.
    By afrodom in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 03-06-2010, 05:04 PM
  3. Why my threads don't run simultaneously?
    By Gilvan Justino in forum New To Java
    Replies: 7
    Last Post: 01-16-2010, 01:43 AM
  4. Executing .exe from java
    By rp181 in forum Advanced Java
    Replies: 6
    Last Post: 09-21-2009, 03:32 PM
  5. Replies: 1
    Last Post: 12-21-2008, 09:06 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
  •