Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paralal Thread Print round robin - Solution #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,111 @@ public class MultiThreadPrint {

public static void main(String[] args) {
/*
* print 1-100 3 times in 3 "parallel" threads
Th1 - 1
Th2 - 1
Th3 - 1

Th1 - 2
Th2 - 2
Th3 - 2

Th1 - 3
Th2 - 3
Th3 - 3

*/
DataPrinter printer = new DataPrinter();
Thread th1 = new SeqGenerator(printer);
Thread th2 = new SeqGenerator(printer);
Thread th3 = new SeqGenerator(printer);

th1.start();
th2.start();
th3.start();


}

public static class SeqGenerator extends Thread {
DataPrinter printer;

int endVal =0;

Runnable r = new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
public SeqGenerator(int n) {
endVal = n;
}

public SeqGenerator(DataPrinter pp) {
printer = pp;
printer.registerThread(this.getName());
}

@Override
public void run() {
for (int i=1;i<=10;i++) {
try {
printer.print(this.getName(), i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};

Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
t1.start();
t2.start();
t3.start();
}
}
public static class DataPrinter {
int curThread = 0;

List<String> threads = new ArrayList<>();
public void registerThread(String threadId) {
threads.add(threadId);
System.out.println(threadId);
}

public synchronized void print(String thId, int val) throws InterruptedException {
while(!threads.get(curThread).equals(thId)) {
wait();
}

System.out.println("Thread " + thId + " prints " + val);

curThread= (curThread + 1) % threads.size();
notifyAll();
}
}
/**
Output

/**
Thread 0 prints 1
Thread 1 prints 1
Thread 2 prints 1
Thread 0 prints 2
Thread 1 prints 2
Thread 2 prints 2
Thread 0 prints 3
Thread 1 prints 3
Thread 2 prints 3
Thread 0 prints 4
Thread 1 prints 4
Thread 2 prints 4
Thread 0 prints 5
Thread 1 prints 5
Thread 2 prints 5
Thread 0 prints 6
Thread 1 prints 6
Thread 2 prints 6
Thread 0 prints 7
Thread 1 prints 7
Thread 2 prints 7
Thread 0 prints 8
Thread 1 prints 8
Thread 2 prints 8
Thread 0 prints 9
Thread 1 prints 9
Thread 2 prints 9
Thread 0 prints 10
Thread 1 prints 10
Thread 2 prints 10


* ASSIGNMENT (PROJECT CLASS 01)
*
* Write more ways of achieving the above goal.
Expand Down