Results 1 to 7 of 7
  1. #1
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default Concurrency in thread creation and execution

    Hi Experts,

    I want a separate class that would manage thread creation.. also, Main class will take a parameter that is how many threads can run in parallel… if I have 10 objects but 4 threads then 5th one should start only after one of the first 4 is complete.. and so on.

    Can anybody help me ..how to implement such kind of scenario.

    I have attached a sample code which will create 10 thread and runs in parallel, But i need it little different , thread will be create by another class and Main class will take only 4 thread at a time.
    After finishing 4th one 5th thread will start.

    Bellow is the code:::

    class MyRunnable implements Runnable {
    int name;

    MyRunnable(int i) {

    name = i;
    System.out.println("Thraed:" +name);

    }


    public void run() {

    try {

    for (int j = 0; j < 3; j++) {
    System.out.println( name + " : " + " " +j);
    Thread.sleep(100);
    }
    }
    catch(InterruptedException e)
    {

    System.out.println("Interrupted");
    }
    }
    }


    public class Main {

    public static void main(String[] args) {

    for (int i = 0; i < 10; i++)
    {

    Runnable task = new MyRunnable(i);


    Thread worker = new Thread(task);

    worker.start();

    }


    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Have you considered using a thread pool?

  3. #3
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    May be Thread Pool will work but... just wanted to know how to write a separate class which will create thread and it will be taken by main class?

    Just new to java..may be my question is little silly..

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The class that creates the threads (in effect it *is* a thread pool) probably won't return the thread that it has created. This is because it may not be possible for the Runnable to run at the time the thread request was made and the thread in question will not be available until some time in the future. Basically main() will create the Runnable instance and then call some method in the other class intending that the runnable be run as soon as possible.

    Basically that method is what you must write: it takes a Runnable argument and either starts a thread for it or remembers that the runnable is "pending". As a previous runnable finishes the pool will check to see if any other runnables are waiting to be run. In carrying out these two things the class must take care of any synchronisation that is necessary.

    An overview of what's involved in writing such a class is given in a paper on IBM's developerworks site: Thread pools and work queues. Not a "new 2 Java" problem (imho) - but an interesting one! The author of that paper more or less suggests what I did: use the builtin classes from java.util.concurrent. There is also a link to Doug Lea's authoratative book on the subject (Concurrent Programming in Java) - he was the author of java.util.concurrent.

    -----

    For what it's worth here's the lazy (or wise) method of employing the builtin library methods:

    Java Code:
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadPoolEg {
    	public static void main(String[] args) {
    			// create a bunch of tasks - the first four
    			// will be started and then the pool will
    			// wait before launching others
    		ExecutorService pool = Executors.newFixedThreadPool(4);
    		for(int i = 0; i < 10; i++) {
    			pool.submit(new MyRunnable(i));
    		}
    		try {
    			pool.shutdown();
    			pool.awaitTermination(20L, TimeUnit.SECONDS);
    		} catch(InterruptedException e) {
    			e.printStackTrace();
    		} finally {
    			pool.shutdownNow();
    		}
    	}
    }
    
    class MyRunnable implements Runnable {
    	private int id;
    
    	public MyRunnable(int id) {
    		this.id = id;
    	}
    
    	public void run() {
    		System.out.println(id + " started");
    		//if(id == 3) {
    		//	int bad = 1 / 0;
    		//}
    		try {
    			//if(id == 8) {
    			//	while(true) {
    			//		Thread.sleep(300);
    			//	}
    			//}
    			for(int j = 0; j < 3; j++) {
    				System.out.println(id + ": " + j);
    				Thread.sleep(300);
    			}
    		} catch(InterruptedException e) {
    			System.out.println(id + " interrupted");
    		}
    		System.out.println(id + " finished");
    	}
    }
    I'm no expert in this stuff - just worked from the javadocs linked to from the tutorial already mentioned - but I'm sure someone will chime in if the example above is Bad in some way.
    Last edited by pbrockway2; 06-11-2011 at 07:45 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I should have added that submit() does a little more than "run as soon as possible" - it also returns a thing which is an instance of Future. The thing it returns has methods that allow you to interact with the runnable like cancel() and isDone(). The same framework can be used with things that are like runnable but actually return a value when they are done (an instance of Callable) which is very useful.

  6. #6
    krishanu is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Hi Pbrockway2,

    Thanks a lot for your help!!!!!1

    I have understood the thing now!!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. thread execution one after another
    By turanan in forum New To Java
    Replies: 16
    Last Post: 05-08-2012, 01:11 PM
  2. make thread to pause execution
    By Saran185 in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-16-2011, 06:01 AM
  3. Thread Execution
    By sunilragidi in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-07-2010, 04:38 PM
  4. Replies: 10
    Last Post: 11-29-2010, 03:02 PM
  5. Thread concurrency .. What am I missing?
    By Ebodee in forum Advanced Java
    Replies: 5
    Last Post: 02-01-2010, 08:03 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
  •