Casey Casey - 5 months ago 8
Java Question

How to make multiple threads run parallally such that execution of threads takes place one after the other

I am trying to acheive parallal thread execution. for example


t1, t2, and t3 are three threads , each performing a task to print
multiplication table. I want to execute them in a sequence


--------------------------------
| t1 | t2 | t3 |
--------------------------------
| 2 | 3 | 4 |
| 4 | 6 | 8 |
| 6 | 9 | 12 |
| .. | .. | .. |
|------------------------------|


execution sequence : t1--> t2--> t3 --> t1

so far, i am able to create three thread that does task independently

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SynchronizationExample1 {
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
for(int i=1; i<=10; i++){
es.execute(new Task(i));
}
es.shutdown();

while(!es.isTerminated()){

}
System.out.println("finished all task");
}
}


Task class

public class Task implements Runnable {
private int i;
public Task(int i){
this.i = i;
}

@Override
public void run() {
System.out.println(Thread.currentThread().getName()+ "Start counter = " + i);
processMessage();
System.out.println(Thread.currentThread().getName()+ "End counter");
}

public void processMessage(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}


output :

pool-1-thread-1Start counter = 1
pool-1-thread-3Start counter = 3
pool-1-thread-2Start counter = 2
pool-1-thread-1End counter
pool-1-thread-1Start counter = 4
pool-1-thread-3End counter
pool-1-thread-3Start counter = 5
pool-1-thread-2End counter
pool-1-thread-2Start counter = 6
pool-1-thread-1End counter
pool-1-thread-1Start counter = 7
pool-1-thread-3End counter
pool-1-thread-3Start counter = 8
pool-1-thread-2End counter
pool-1-thread-2Start counter = 9
pool-1-thread-1End counter
pool-1-thread-1Start counter = 10
pool-1-thread-3End counter
pool-1-thread-2End counter
pool-1-thread-1End counter
finished all task

Answer

I am trying to acheive parallal thread execution. for example t1, t2, and t3 are three threads , each performing a task to print multiplication table. I want to execute them in a sequence.

That is a contradiction in terms.

You can do things in parallel or in sequence, but you cannot do both at the same time. (It is like running and riding a bicycle at the same time!)


As @Thilo states, the simple, practical (and best) solution is not to do this with multiple threads. Just use one thread and a for loop.

If you are intent on doing this using multiple threads, then you need the threads to synchronize. (That is what appears to be the problem with your current code. You have no synchronization ... just three "free running" threads and sleep() calls. That approach is never going to be reliable.)

For instance,

  • thread 1 prints a line, tells thread 2 - "your turn", and waits for its turn
  • thread 2 prints a line, tells thread 3 - "your turn", and waits for its turn
  • thread 3 prints a line, tells thread 1 - "your turn", and waits for its turn
  • and so on

until you get to the end. (You will need to do something special then ...)

This notification could take the form of 3 separate "channels" (primitive mutexes, latches, semaphores, queues, whatever) for each three pairs of threads, or a single "channel" where each thread wait for its turn.

(There are other ways to solve this too ... but we don't need to go into that here.)

Comments