Task - Multithreading - Print Even Odd #41
Replies: 39 comments
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`package MultiThreading; public class EvenOddSynchronized { }` |
Beta Was this translation helpful? Give feedback.
-
|
`package threads_in_java; class Numbers { } class Thread1 extends Thread { } class Thread2 extends Thread { } public class SynchronizationTask { }` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
` } } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.lang.InterruptedException; class EvenOdd { } public class ThreadsEvenOdd { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package com.lingu;
class T{
static int LIMIT=40;
int i=1;
synchronized void printeven()
{
while(i<LIMIT)
{
while(i%2==0)
{
try {
wait();
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println(i++);
notifyAll();
}
}
synchronized void printodd()
{
while(i<LIMIT)
{
while(i%2==1)
{
try {
wait();
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println(i++);
notifyAll();
}
}
}
class C{
public static void main(String[] args) {
T t=new T();
new Thread()
{
@Override
public void run()
{
t.printeven();
}
}.start();
new Thread()
{
@Override
public void run()
{
t.printodd();
}
}.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
interface maxLimit{
int max = 10;
}
class EvenThread extends Thread implements maxLimit{
@Override
public void run(){
for(int i = 0; i < max; i++){
if(i % 2 == 0){
System.out.println("Even number");
}
}
}
}
class OddThread extends Thread implements maxLimit{
@Override
public void run(){
for(int i = 0; i < max; i++){
if(i % 2 != 0){
System.out.println("Odd number");
}
}
}
}
public class Discussion41{
public static void main(String[] args) {
EvenThread t1 = new EvenThread();
OddThread t2 = new OddThread();
t1.start();
t2.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
Codeclass A{
int i = 1;
public synchronized void printEven() {
while (this.i <= 50) {
if (i % 2 == 0) {
System.out.println("Even :" + this.i++);
notifyAll();
}
else {
try {
wait();
}
catch (InterruptedException e) {
}
}
}
}
public synchronized void printOdd(){
while(this.i <= 50) {
if (i % 2 != 0) {
System.out.println("Odd : " + this.i++);
notifyAll();
}
else {
try {
wait();
}
catch (InterruptedException e) {
}
}
}
}
}
public class EvenOddNumbers {
public static void main(String[] args) {
A a1 = new A();
new Thread(){
public void run() {
a1.printEven();
}
}.start();
new Thread(){
public void run() {
a1.printOdd();
}
}.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
package p1; public class Problem41 extends Thread{ } |
Beta Was this translation helpful? Give feedback.
-
class A{
int i = 1;
public synchronized void printEven() {
while (this.i <= 50) {
if (i % 2 == 0) {
System.out.println("Even :" + this.i++);
notifyAll();
}
else {
try {
wait();
}
catch (InterruptedException e) {
}
}
}
}
public synchronized void printOdd(){
while(this.i <= 50) {
if (i % 2 != 0) {
System.out.println("Odd : " + this.i++);
notifyAll();
}
else {
try {
wait();
}
catch (InterruptedException e) {
}
}
}
}
}
public class EvenOddNumbers {
public static void main(String[] args) {
A a1 = new A();
new Thread(){
public void run() {
a1.printEven();
}
}.start();
new Thread(){
public void run() {
a1.printOdd();
}
}.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
package multithreading;
public class EvenOdd{
static int limit = 10;
int i = 1;
synchronized void printEven() {
while (i <= limit) {
if (i % 2 == 0) {
System.out.println("Even " + i);
i++;
notifyAll();
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
synchronized void printOdd() {
while (i <= limit) {
if (i % 2 != 0) {
System.out.println("Odd " + i);
i++;
notifyAll();
} else
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
class Thread1 extends Thread{
@Override
public void run() {
Program41 even=new Program41();
even.evenNumber();
}
}
class Thread2 implements Runnable{
@Override
public void run() {
Program41 odd=new Program41();
odd.oddNumber();
}
}
public class Program41 {
static int limit=100;
public static void main(String[] args) {
Thread1 t1=new Thread1();
t1.start();
Thread2 r=new Thread2();
Thread t2=new Thread(r);
t2.start();
}
synchronized void evenNumber() {
for(int i=0;i<limit;i++) {
if(i%2==0) {
System.out.println("Even "+i);
}
}
}
synchronized void oddNumber() {
for(int i=0;i<limit;i++) {
if(i%2!=0) {
System.out.println("Odd "+i);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
class printOddEven {
public synchronized void displayEven(int num) {
for(int i = 1 ; i <= num ; i++) {
if( i % 2 == 0) {
System.out.println(i + " Even Number");
notify();
}else {
try {
wait();
} catch (InterruptedException e) {
}
}
}
}
public synchronized void displayOdd(int num) {
for(int i = 1 ; i <= num ; i++) {
if( i % 2 != 0) {
System.out.println(i + " Odd Number");
notify();
}else {
try {
wait();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
public class Driver {
public static void main(String[] args) {
printOddEven p1 = new printOddEven();
new Thread() {
public void run() {
p1.displayEven(20);
}
}.start();
new Thread() {
public void run() {
p1.displayOdd(20);
}
}.start();
}
} |
Beta Was this translation helpful? Give feedback.
-
class Thread1 extends Thread{
@Override
public void run() {
Task41 even = new Task41();
even.evenNumber();
}
}
class Thread2 implements Runnable{
@Override
public void run() {
Task41 odd = new Task41();
odd.oddNumber();
}
}
public class Task41 {
static int limit = 50;
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.start();
Thread2 r = new Thread2();
Thread t2 = new Thread(r);
t2.start();
}
synchronized void evenNumber() {
for(int i = 0; i<limit; i++) {
if(i%2 == 0) {
System.out.println("Even" +i);
}
}
}
synchronized void oddNumber() {
for(int i = 0; i<limit; i++) {
if(i%2!=0) {
System.out.println("odd "+i);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
class Thread1 extends Thread{
@Override
public void run() {
Task41 even = new Task41();
even.evenNumber();
}
}
class Thread2 implements Runnable{
@Override
public void run() {
Task41 odd = new Task41();
odd.oddNumber();
}
}
public class Task41 {
static int limit = 50;
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.start();
Thread2 r = new Thread2();
Thread t2 = new Thread(r);
t2.start();
}
synchronized void evenNumber() {
for(int i = 0; i<limit; i++) {
if(i%2 == 0) {
System.out.println("Even" +i);
}
}
}
synchronized void oddNumber() {
for(int i = 0; i<limit; i++) {
if(i%2!=0) {
System.out.println("odd "+i);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
WAP to print the even and odd numbers to a value LIMIT using two threads.
Hint - You can use a static variable, a non-static counter, two functions (one for printing even numbers, one for printing odd numbers), the synchronized block, and the notifyAll() method.
Beta Was this translation helpful? Give feedback.
All reactions