Reputation: 437
I need to run two threads parallel.
1) PrimeThread which calculates prime numbers from 1 to 100. 2) evenThread which calculates even numbers from 1 to 100.
I had implemented logic of primes and evens. Don't focus on it. My question is,
I want to print numbers in following manner. 10 primes 10 evens 10 primes 10 evens and rest of them.
Is that possible???
I used sleep() method but it doesn't work. please help me. Thanks.
Upvotes: 0
Views: 164
Reputation: 780
Look this example, its shows you how to use thread with synchronize manner...
In this I will print "A,A,A,.....B,B,B....C,C,C" & VICE VERSA INSTEAD OF RANDMOLY "C,B,A,B,C,A,B" etc...
//Use of synchronize
class A
{
char c[] = {'A', 'B', 'C'};
synchronized void show (int index)
{
try
{
for(int i=0; i<3; i++)
{
System.out.println(c[index]);
Thread.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class B extends Thread
{
int index;
A ob1;
void set (int i)
{
index =i;
}
public void run()
{
ob1.show(index);
}
public static void main (String[] args)
{
A ob2 = new A ();
B b1 = new B ();
B b2 = new B ();
B b3 = new B ();
b1.set(0);
b2.set(1);
b3.set(2);
b1.ob1 = ob2;
b2.ob1 = ob2;
b3.ob1 = ob2;
b1.start();
b2.start();
b3.start();
}
}
I hope this example help for you as it is quite similar to your question...
Upvotes: 0
Reputation: 328737
I need to run two threads parallel.
[...]
I want to print numbers in following manner. 10 primes 10 evens 10 primes 10 evens and rest of them.
This seems contradictory: the output you want means that what you really want is to run the tasks sequentially, but in an interleaved way. Why don't you create 2 methods: printNextPrimes(int number)
and printNextEvens(int number)
and call them in a single threaded loop:
for (int i = 0; i < 10; i++) {
printNextPrimes(10);
printNextEvens(10);
}
Upvotes: 1
Reputation: 136062
Something like this:
public class Test {
static Object lock = new Object();
static boolean printPrime = true;
static boolean printEven;
static class PrimeTread extends Thread {
public void run() {
int n = 0;
while (n < 100) {
synchronized (lock) {
if (printPrime) {
for (int i = 0; i < 10; i++) {
System.out.println(nextPrime());
n++;
}
printPrime = false;
printEven = true;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
private int nextPrime() {
return 31;
}
}
static class EvenTread extends Thread {
public void run() {
int n = 0;
while (n < 100) {
synchronized (lock) {
if (printEven) {
for (int i = 0; i < 10; i++) {
System.out.println(nextEven());
n++;
}
printPrime = true;
printEven = false;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
private int nextEven() {
return 2;
}
}
public static void main(String[] args) {
new EvenTread().start();
new PrimeTread().start();
}
}
Upvotes: 0
Reputation: 16215
It sounds like you're looking for a Semaphore
Each thread will have its own semaphore. After outputting 10 numbers, it will release the semaphore owned by the other thread, and acquire its own. This will make the other thread start again, and block that thread until it is released again.
Upvotes: 0