Results 1 to 5 of 5
  1. #1
    gamodg is offline Member
    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Default Serial Execution of threads

    Hi

    I have three threads

    T1 prints 1 4 7 10...
    T2 prints 2 5 8 11...
    T3 prints 3 6 9 13...

    I want to print 1 2 3 4 5 6 7 8 9 10 11 12.....

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Multiple thread not guaranteed by the thread pool that it'll executed in same order that you start.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gamodg View Post
    Hi

    I have three threads

    T1 prints 1 4 7 10...
    T2 prints 2 5 8 11...
    T3 prints 3 6 9 13...

    I want to print 1 2 3 4 5 6 7 8 9 10 11 12.....
    Just use a single thread that prints 1, 2, 3 etc. because otherwise you'll need quite a bit of synchronization and scheduling for effectively just executing one single thread.

    kind regards,

    Jos

  4. #4
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    Maybe this helps:
    Java Code:
    public class threadClass
    {
    	int i=1;
    	public threadClass()
    	{
    		Thread t1 = new Thread(run1);
    		Thread t2 = new Thread(run2);
    		Thread t3 = new Thread(run3);
    		
    		t1.start();
    		t2.start();
    		t3.start();
    	}
    	
    	Runnable run1 = new Runnable(){
    		public void run()
    		{
    			while(true)
    			{
    				if((i%3)==0){
    					System.out.println("thread 1: " + i++);
    				}
    			}
    		}
    	};
    	
    	Runnable run2 = new Runnable(){
    		public void run()
    		{
    			while(true)
    			{
    				if((i%3)==1){
    					System.out.println("thread 2: " + i++);
    				}
    			}
    		}
    	};
    	
    	Runnable run3 = new Runnable(){
    		public void run()
    		{
    			while(true)
    			{
    				if((i%3)==2){
    					System.out.println("thread 3: " + i++);
    				}
    			}
    		}
    	};
    	
    	public static void main(String... args)
    	{
    		new threadClass();
    	}
    }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 1
    Last Post: 07-30-2010, 08:29 AM
  2. JAR execution
    By patoh in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 09:09 AM
  3. execution of threads
    By jyotigupta6 in forum Threads and Synchronization
    Replies: 5
    Last Post: 11-16-2008, 12:28 AM
  4. Threads execution stop
    By jyotigupta6 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-31-2008, 01:41 PM
  5. slow execution and flickery graphics. too many threads?
    By lost_in_java in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-23-2008, 02:26 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
  •