0% found this document useful (0 votes)
11 views

task synchronized method even_odd

The document contains Java code that implements a synchronized method to print even and odd numbers using threads. The Even_Odd class has a synchronized method that takes a boolean to determine whether to print even or odd numbers up to a specified limit. The Main_Even_Odd class manages the execution of Even and Odd threads, ensuring that even numbers are printed before odd numbers.

Uploaded by

mr.ompawar9834
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

task synchronized method even_odd

The document contains Java code that implements a synchronized method to print even and odd numbers using threads. The Even_Odd class has a synchronized method that takes a boolean to determine whether to print even or odd numbers up to a specified limit. The Main_Even_Odd class manages the execution of Even and Odd threads, ensuring that even numbers are printed before odd numbers.

Uploaded by

mr.ompawar9834
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

package Synchronization.

Synchronized_method;

public class Even_Odd

synchronized void even_odd(boolean b,int num) // Synchronized method

for (int i = 1;i <= num;i++) {

if (b == true) {

if (i % 2 == 0) {

System.out.print(i + " , ");

} else {

if (i % 2 != 0) {

System.out.print(i + " , ");

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

System.out.println();

}
package Synchronization.Synchronized_method;

import java.util.Scanner;

public class Even extends Thread

Scanner sc = new Scanner(System.in);

Even_Odd t;

Even(Even_Odd t)

this.t=t;

@Override

public void run()

System.out.println("Enter a number to print Even numbers : ");

int num = sc.nextInt();

System.out.println("Even Numbers : ");

t.even_odd(true,num);

}
package Synchronization.Synchronized_method;

import java.util.Scanner;

public class Odd extends Thread

Scanner sc = new Scanner(System.in);

Even_Odd t;

Odd(Even_Odd t)

this.t=t;

@Override

public void run()

System.out.println("Enter a number to print Odd numbers : ");

int num = sc.nextInt();

System.out.println("Odd Numbers : ");

t.even_odd(false,num);

}
package Synchronization.Synchronized_method;

public class Main_Even_Odd

public static void main(String[] args)

Even_Odd obj1 = new Even_Odd();

Even obj2 = new Even(obj1);

obj2.start();

try

obj2.join();

catch (InterruptedException e)

e.printStackTrace();

Odd obj3 = new Odd(obj1);

obj3.start();

try

obj2.join();

catch (InterruptedException e)

e.printStackTrace();

}
OUTPUT :

Enter a number to print Even numbers :

Even Numbers :

2,4,

Enter a number to print Odd numbers :

10

Odd Numbers :

1,3,5,7,9,

You might also like