Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3adaf6b

Browse files
author
C5141506
committed
numbers generation by 3 threads
1 parent d34d081 commit 3adaf6b

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package java_problem.multi_threading.parallel;
2+
3+
public class NumberGeneratorHandler implements Runnable {
4+
private ParallelNumberGeneratorTask parallelNumberGeneratorTask;
5+
private int result;
6+
7+
public NumberGeneratorHandler(ParallelNumberGeneratorTask parallelNumberGeneratorTask, int result) {
8+
this.parallelNumberGeneratorTask = parallelNumberGeneratorTask;
9+
this.result = result;
10+
}
11+
12+
@Override
13+
public void run() {
14+
parallelNumberGeneratorTask.printNumber(result);
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package java_problem.multi_threading.parallel;
2+
3+
public class NumberGeneratorRunner {
4+
public static void main(String[] args) {
5+
int noOfThread = 3;
6+
int totalNumberInSequence = 10;
7+
ParallelNumberGeneratorTask parallelNumberGeneratorTask = new ParallelNumberGeneratorTask(noOfThread, totalNumberInSequence);
8+
9+
//thread-1 should print number that has reminder 1
10+
Thread t1 = new Thread(new NumberGeneratorHandler(parallelNumberGeneratorTask, 1), "Thread-1");
11+
//thread-2 should print number that has reminder 2
12+
Thread t2 = new Thread(new NumberGeneratorHandler(parallelNumberGeneratorTask, 2), "Thread-2");
13+
//thread-3 should print number that has reminder 0
14+
Thread t3 = new Thread(new NumberGeneratorHandler(parallelNumberGeneratorTask, 0), "Thread-3");
15+
16+
t1.start();
17+
t2.start();
18+
t3.start();
19+
20+
21+
}
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package java_problem.multi_threading.parallel;
2+
3+
public class ParallelNumberGeneratorTask {
4+
private int number = 1;
5+
private final int totalNumberInSequence;
6+
private final int noOfThread;
7+
8+
ParallelNumberGeneratorTask(int noOfThread, int totalNumberInSequence) {
9+
this.noOfThread = noOfThread;
10+
this.totalNumberInSequence = totalNumberInSequence;
11+
}
12+
13+
public void printNumber(int result) {
14+
synchronized (this) {
15+
while (number < totalNumberInSequence-1) {
16+
while (number % noOfThread != result) {
17+
try {
18+
wait();
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
//thread get cpu to perform his operation
24+
System.out.print(Thread.currentThread().getName()+" ");
25+
System.out.print(""+number+" ");
26+
//increase no and print same if matches to specific thread
27+
number++;
28+
//Wakes up all threads that are waiting on this object's monitor.
29+
// A thread waits on an object's monitor by calling one of the wait methods.
30+
notifyAll();
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /