'How do I ensure that one thread always run ahead of other in java? Given two threads. I need to make sure that Thread 1 always leads Thread 2
Given two threads. Thread 1 : prints from 1 to 100 Thread 2 : prints from 1 to 100 I need to make sure that Thread 1 never prints any number which is not yet printed by Thread 2.
Please help me with this. Thanks in advance. (-- was asked in my interview with Wissen Technology)
I tried below approach.
package threadTutor;
public class ThreadLeader {
public static void main(String[] args) throws InterruptedException {
Leader lead = new Leader(100);
Follower foll = new Follower(100);
foll.start();
lead.start();
foll.join();
lead.join();
}
}
class Leader extends Thread{
static int start;
static int end;
static int pos;
Leader(int end){
Leader.end = end;
}
public void run() {
for(int i=start;i<end;i++) {
System.out.println("Leader is printing "+i);
Leader.pos=i;
}
}
}
class Follower extends Thread{
static int start;
static int end;
static int pos;
Follower(int end){
Follower.end = end;
}
public void run() {
while(true) {
if(Follower.pos<=Leader.pos) {
System.out.println("Follower is printing "+pos);
pos++;
}
if(Follower.pos==Follower.end) break;
}
}
}
but ofcourse infinite while loop is not a good way of doing things. Please help with a better approach.
Solution 1:[1]
This is a consumer-producer problem. Folloing is a blocking queue implementation. Other options:https://www.geeksforgeeks.org/producer-consumer-solution-using-threads-java/
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ThreadLeader {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
Leader lead = new Leader(queue, 100);
Follower foll = new Follower(queue, 100);
foll.start();
lead.start();
foll.join();
lead.join();
}
}
class Leader extends Thread {
private BlockingQueue<Integer> queue;
private int num;
Leader(BlockingQueue<Integer> queue, int num) {
this.queue = queue;
this.num = num;
}
public void run() {
for (int i = 1; i <= num; i++) {
System.out.println("Leader is printing " + i);
queue.offer(i);
}
}
}
class Follower extends Thread {
private BlockingQueue<Integer> queue;
private int num;
Follower(BlockingQueue<Integer> queue, int num) {
this.queue = queue;
this.num = num;
}
public void run() {
for (int i = 0; i < num; i++){
try {
System.out.println("Follower is printing " + queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
