Print even and odd numbers using threads in java - Java-fries
Problem: How to print even and odd numbers using threads in Java? Two threads should be used to print numbers. One thread should print even numbers and other should print odd numbers. Numbers should be printed sequentially .i.e. 1, 2, 3, 4..
package com.javafries.thread;
public class EvenOddNumberPrinter {
private static class NumberPrinter {
// To check if even number is printed or not.
private boolean isEvenPrinted = true;
public void printOdd(int number) throws InterruptedException {
// Get a lock on NumberPrinter
synchronized (this) {
// Wait until even is not printed.
if (!isEvenPrinted)
wait();
System.out.println(number);
isEvenPrinted = false;
// Notify the other waiting thread which is waiting on
// NumberPrinter
// Other thread will get out of waiting state
notify();
}
}
public void printEven(int number) throws InterruptedException {
synchronized (this) {
if (isEvenPrinted)
wait();
System.out.println(number);
isEvenPrinted = true;
notify();
}
}
}
private static class OddNumberGenerator implements Runnable {
private NumberPrinter q;
private int max;
public OddNumberGenerator(NumberPrinter q, int max) {
this.q = q;
this.max = max;
}
@Override
public void run() {
for (int i = 1; i < max; i = i + 2) {
try {
q.printOdd(i);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
http://www.coderpanda.com/communication-between-threads-in-java/
private static class EvenNumberGenerator implements Runnable {
private NumberPrinter printer;
private int max;
public EvenNumberGenerator(NumberPrinter printer, int max) {
this.printer = printer;
this.max = max;
}
@Override
public void run() {
for (int i = 2; i <= max; i = i + 2) {
try {
printer.printEven(i);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
int maxNumber = 10;
NumberPrinter printer = new NumberPrinter();
new Thread(new EvenNumberGenerator(printer, maxNumber)).start();
new Thread(new OddNumberGenerator(printer, maxNumber)).start();
}
}
public class EvenRunnable implements Runnable {
private int number = 2;
private Object shared = null;
public EvenRunnable(Object object) {
shared = object;
}
public void run() {
while (number < 50) {
synchronized (shared) {
System.out.println("Even number = " + number);
number = number + 2;
try {
Thread.sleep(500); //only to view sequence of execution
shared.notify();
shared.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class OddRunnable implements Runnable {
int oddNumber = 1;
private Object shared = null;
public OddRunnable(Object object) {
shared = object;
}
public void run() {
while (oddNumber < 50) {
synchronized (shared) {
System.out.println("Odd number = " + oddNumber);
oddNumber = oddNumber + 2;
try {
Thread.sleep(500); // only to view the sequence of execution
shared.notify();
shared.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class Main {
public Main(){
}
public static void main(String[] args) {
Object shared = new Object();
EvenRunnable evenRunnable = new EvenRunnable(shared);
OddRunnable oddRunnable = new OddRunnable(shared);
Thread evenThread = new Thread(evenRunnable, "evenThread");
Thread oddThread = new Thread(oddRunnable, "oddThread");
oddThread.start();
evenThread.start();
}
}
http://anujprashar.blogspot.com/2014/11/printing-odd-even-number-alternately.html
class Counter{
//The int variable that is used by both threads.
public int count = 1;
}
class EvenTask implements Runnable{
private Counter counter;
public EvenTask(Counter counter){
this.counter = counter;
}
@Override
public void run() {
while (true) {
synchronized (counter) {
//If counter.num is odd then even thread will release lock and goes to wait state
//so that odd thread can print.
if(counter.count % 2!=0){
try {
counter.notify();
counter.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Control will come here if odd thread has released lock
//or if number is even.
System.out.println(Thread.currentThread().getName()+"-"+counter.count);
counter.count++;
try {
Thread.sleep(200);//Some ms of sleep so that output is not very fast.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class OddTask implements Runnable{
private Counter counter;
public OddTask(Counter counter){
this.counter = counter;
}
@Override
public void run() {
while (true) {
synchronized (counter) {
//If counter.num is even then odd thread will release lock and goes to wait state
//so that even thread can print.
if(counter.count % 2==0){
try {
counter.notify();
counter.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Control will come here if even thread has released lock
//or if number is odd.
System.out.println(Thread.currentThread().getName()+"-"+counter.count);
counter.count++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
http://www.somanyword.com/2013/11/printing-even-and-odd-using-two-threads-in-java/
Read full article from Print even and odd numbers using threads in java - Java-fries
Problem: How to print even and odd numbers using threads in Java? Two threads should be used to print numbers. One thread should print even numbers and other should print odd numbers. Numbers should be printed sequentially .i.e. 1, 2, 3, 4..
package com.javafries.thread;
public class EvenOddNumberPrinter {
private static class NumberPrinter {
// To check if even number is printed or not.
private boolean isEvenPrinted = true;
public void printOdd(int number) throws InterruptedException {
// Get a lock on NumberPrinter
synchronized (this) {
// Wait until even is not printed.
if (!isEvenPrinted)
wait();
System.out.println(number);
isEvenPrinted = false;
// Notify the other waiting thread which is waiting on
// NumberPrinter
// Other thread will get out of waiting state
notify();
}
}
public void printEven(int number) throws InterruptedException {
synchronized (this) {
if (isEvenPrinted)
wait();
System.out.println(number);
isEvenPrinted = true;
notify();
}
}
}
private static class OddNumberGenerator implements Runnable {
private NumberPrinter q;
private int max;
public OddNumberGenerator(NumberPrinter q, int max) {
this.q = q;
this.max = max;
}
@Override
public void run() {
for (int i = 1; i < max; i = i + 2) {
try {
q.printOdd(i);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
http://www.coderpanda.com/communication-between-threads-in-java/
private static class EvenNumberGenerator implements Runnable {
private NumberPrinter printer;
private int max;
public EvenNumberGenerator(NumberPrinter printer, int max) {
this.printer = printer;
this.max = max;
}
@Override
public void run() {
for (int i = 2; i <= max; i = i + 2) {
try {
printer.printEven(i);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
int maxNumber = 10;
NumberPrinter printer = new NumberPrinter();
new Thread(new EvenNumberGenerator(printer, maxNumber)).start();
new Thread(new OddNumberGenerator(printer, maxNumber)).start();
}
}
public class EvenRunnable implements Runnable {
private int number = 2;
private Object shared = null;
public EvenRunnable(Object object) {
shared = object;
}
public void run() {
while (number < 50) {
synchronized (shared) {
System.out.println("Even number = " + number);
number = number + 2;
try {
Thread.sleep(500); //only to view sequence of execution
shared.notify();
shared.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class OddRunnable implements Runnable {
int oddNumber = 1;
private Object shared = null;
public OddRunnable(Object object) {
shared = object;
}
public void run() {
while (oddNumber < 50) {
synchronized (shared) {
System.out.println("Odd number = " + oddNumber);
oddNumber = oddNumber + 2;
try {
Thread.sleep(500); // only to view the sequence of execution
shared.notify();
shared.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class Main {
public Main(){
}
public static void main(String[] args) {
Object shared = new Object();
EvenRunnable evenRunnable = new EvenRunnable(shared);
OddRunnable oddRunnable = new OddRunnable(shared);
Thread evenThread = new Thread(evenRunnable, "evenThread");
Thread oddThread = new Thread(oddRunnable, "oddThread");
oddThread.start();
evenThread.start();
}
}
class Counter{
//The int variable that is used by both threads.
public int count = 1;
}
class EvenTask implements Runnable{
private Counter counter;
public EvenTask(Counter counter){
this.counter = counter;
}
@Override
public void run() {
while (true) {
synchronized (counter) {
//If counter.num is odd then even thread will release lock and goes to wait state
//so that odd thread can print.
if(counter.count % 2!=0){
try {
counter.notify();
counter.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Control will come here if odd thread has released lock
//or if number is even.
System.out.println(Thread.currentThread().getName()+"-"+counter.count);
counter.count++;
try {
Thread.sleep(200);//Some ms of sleep so that output is not very fast.
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class OddTask implements Runnable{
private Counter counter;
public OddTask(Counter counter){
this.counter = counter;
}
@Override
public void run() {
while (true) {
synchronized (counter) {
//If counter.num is even then odd thread will release lock and goes to wait state
//so that even thread can print.
if(counter.count % 2==0){
try {
counter.notify();
counter.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Control will come here if even thread has released lock
//or if number is odd.
System.out.println(Thread.currentThread().getName()+"-"+counter.count);
counter.count++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
http://www.somanyword.com/2013/11/printing-even-and-odd-using-two-threads-in-java/
public class EvenOddTest { public static void main(String[] args) { EvenOddPrinter printer = new EvenOddPrinter(); Thread even = new Thread(new PrintTask(printer, 100, true)); Thread odd = new Thread(new PrintTask(printer, 100, false)); odd.start(); even.start(); } } class PrintTask implements Runnable{ int max = 0; boolean isEven = false; EvenOddPrinter p = null; int num = 0; public PrintTask(EvenOddPrinter p, int max, boolean isEven) { this.p = p; this.max = max; this.isEven = isEven; } public void run() { num = (isEven) ? 2 : 1; while (num <= max) { if (isEven){ p.printEven(num); } else { p.printOdd(num); } num += 2; } } } class EvenOddPrinter { boolean isOdd = false; public synchronized void printEven(int number) { while (!this.isOdd) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Even Number : " + number); this.isOdd = false; notify(); } public synchronized void printOdd(int number) { while (this.isOdd) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Odd Number : " + number); this.isOdd = true; notify(); } }http://rohandhapodkar.blogspot.com/2012/04/print-odd-and-even-numbers-using-two.html
Read full article from Print even and odd numbers using threads in java - Java-fries