Closed zhamri closed 6 years ago
Source Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mythread;
/**
*
* @author aspire
*/
public class Test {
private static boolean oddFlag = true;
int count = 10;
private void oddPrinter() {
synchronized (this) {
while(true) {
try {
if(count <= 100) {
if(oddFlag) {
System.out.println(Thread.currentThread().getName() + ": " + count++);
oddFlag = !oddFlag;
notifyAll();
}
else {
wait();
}
}
else {
notify();
break;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void evenPrinter() {
synchronized (this) {
while (true) {
try {
if(count <= 100) {
if(!oddFlag) {
System.out.println(Thread.currentThread().getName() + ": " + count++);
oddFlag = !oddFlag;
notify();
}
else {
wait();
}
}
else {
notify();
break;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException{
final Test test = new Test();
Thread t1 = new Thread(new Runnable() {
public void run() {
test.oddPrinter();
}
}, "Thread 1 Even Printer ");
Thread t2 = new Thread(new Runnable() {
public void run() {
test.evenPrinter();
}
}, "Thread 2 Odd Printer");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Main thread finished");
}
}
Screenshot
public class Issue10{
static Object lock = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
for (int num = 10; num < 101; num = num + 2) {
synchronized (lock) {
System.out.println("even"+" " + num);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
for (int num = 11; num < 101; num = num + 2) {
synchronized (lock) {
System.out.println("odd"+" " + num);
try {
lock.notify();
if(num==100)
break;
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("\nPrinting over");
} catch (Exception e) {
}
}
}
Screenshot
package com.mycompany.issue_10;
public class oddEven {
public static void main (String[] args){
Thread t1 = new Thread (new Runnable() {
@Override
public void run(){
for (int i = 10; i <= 100; i++) {
if (i % 2 != 0) {
System.out.println("Odd number:" +i);
}
}
}});
Thread t2 = new Thread (new Runnable(){
@Override
public void run (){
for (int i = 10; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println("Even number:" +i);
}
}
}});
try {
t1.start();
t2.start();
t1.join();
t2.join();
} catch (Exception e) {
}
}}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.stiw3054.issue09;
/**
*
* @author Saufi
*/
public class Main {
/**
* @param args the command line arguments
*/
static Object lock = new Object();
public static void main(String[] args) {
Thread t1;
t1 = new Thread(() -> {
for (int itr = 10; itr < 101; itr = itr + 2) {
synchronized (lock) {
System.out.println("Even " + itr);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
}
}
}
});
Thread t2;
t2 = new Thread(() -> {
for (int i = 11; i < 101; i = i + 2) {
synchronized (lock) {
System.out.println("Odd " + i);
try {
lock.notify();
if(i==100)
break;
lock.wait();
} catch (InterruptedException e) {
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
} catch (InterruptedException e) {
}
}
}
Sample output
Code
package issue10;
public class Issue10 {
static Object lock = new Object();
public static void main(String[] args) throws InterruptedException {
Issue10 i = new Issue10();
Thread t1 = new Thread(new Runnable() {
public void run() {
i.evenNum();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
i.oddNum();
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
} catch (Exception e) {
}
}
public void evenNum() {
for (int i = 10; i <= 100; i = i + 2) {
synchronized (lock) {
System.out.println("Even \t" + i);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void oddNum() {
for (int j = 11; j <= 100; j = j + 2) {
synchronized (lock) {
System.out.println("Odd \t" + j);
try {
lock.notify();
if (j == 100) {
break;
}
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
output
public class Mythread {
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable1();
Thread t1 = new Thread(r);
t1.start();
t1.join();
Runnable r2 = new Runnable2();
Thread t2 = new Thread(r2);
t2.start();
t2.join();
}
}
class Runnable2 implements Runnable{ public void run(){ for(int i=10;i<101;i++){ if(i%2 == 1)
System.out.println("Odd: " + i);
}
}
}
class Runnable1 implements Runnable{ public void run(){ for(int i=10;i<101;i++){ if(i%2 == 0)
System.out.println("Even: "+ i);
}
}
}
public class Exercise9 {
public static void main(String[] args) throws InterruptedException {
Exercise9 call = new Exercise9();
Thread t1 = new Thread(new Runnable() {
public void run() {
call.oddnumber();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
call.evennumber();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
public void oddnumber() {
int todd=0;
for (int k = 11; k <= 100; k = k + 2)
{
System.out.println("Odd number is: " + k);
todd = todd +1;
}
System.out.println("Total of Odd number is: " + todd);
}
public void evennumber() {
int teven=0;
for (int i = 10; i <= 100; i = i + 2) {
System.out.println("Even number is: " + i);
teven = teven + 1;
}
System.out.println("Total of Even number is: " + teven);
}
}
* @author NUTSYABA
*/
public class issue9 {
public static final int limit = 100;
public static void main(String... args) {
final Counter counterObj = new Counter(100);
Runnable evenNoPrinter = new Runnable() {
public void run() {
int num = 0;
while (true) {
if (num >= limit) {
break;
}
num = counterObj.printNextEven();
}
}
};
Runnable oddNoPrinter = new Runnable() {
public void run() {
int num = 0;
while (true) {
if (num >= limit) {
break;
}
num = counterObj.printNextOdd();
}
}
};
try {
new Thread(oddNoPrinter).start();
new Thread(evenNoPrinter).start();
new Thread(oddNoPrinter).join();
new Thread(evenNoPrinter).join();
} catch (Exception e) {
}
}
}
class Counter {
private int count = 0;
private boolean even = true;
private int upperLimit;
Counter(int limit) {
upperLimit = limit;
}
public synchronized int printNextOdd() {
//Wait until odd is available.
while (even) {
try {
wait();
} catch (InterruptedException e) {
}
}
count++;
if (count <= upperLimit) {
printEven(count);
}
//Toggle status.
even = true;
//Notify even printer that status has changed.
notifyAll();
return count;
}
public synchronized int printNextEven() {
//Wait until even is available.
while (!even) {
try {
wait();
} catch (InterruptedException e) {
}
}
count++;
if (count <= upperLimit) {
printOdd(count);
}
//Toggle status.
even = false;
//Notify odd printer that status has changed.
notifyAll();
return count;
}
public void printOdd(int num) {
System.out.println("ODD : " + num);
}
public void printEven(int num) {
System.out.println("EVEN : " + num);
}
}
package exercise9;
public class Exercise9 {
public static void main(String[] args) throws InterruptedException {
Exercise9 call = new Exercise9();
Thread t1 = new Thread(new Runnable() {
public void run() {
call.oddnumber();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
call.evennumber();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
public void oddnumber() {
int todd=0;
for (int k = 11; k <= 100; k = k + 2)
{
System.out.println("Odd: " + k);
todd = todd +1;
}
System.out.println("Total of Odd number: " + todd);
}
public void evennumber() {
int teven=0;
for (int i = 10; i <= 100; i = i + 2) {
System.out.println("Even: " + i);
teven = teven + 1;
}
System.out.println("Total of Even number: " + teven);
}
}
# Java Code
public class exercise9 {
public static void main(String[] args) {
Object lock = new Object();
exercise9 y = new exercise9();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 10; i < 102; i = i + 2) {
System.out.println("Even Number: " + i);
synchronized (lock) {
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int k = 11; k < 100; k = k + 2) {
System.out.println("Odd Number : " + k);
synchronized (lock) {
try {
lock.notify();
if (k == 101) {
break;
}
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
} catch (Exception e) {
System.out.println("Error ");
}
}
}
# OUTPUT
public class Issues9 {
static Object lock = new Object();
public static void main(String[] args) {
Thread Odd = new Thread(new Runnable() {
public void run() {
for (int x = 10; x < 101; x = x + 2){
synchronized (lock) {
System.out.println(x + " is EVEN number");
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread Even = new Thread(new Runnable() {
public void run() {
for (int x = 11; x < 101; x = x + 2){
synchronized (lock) {
System.out.println(x + " is ODD number");
try {
lock.notify();
if(x==100)
break;
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
Odd.start();
Even.start();
Odd.join();
Even.join();
} catch (Exception e) {
}
}
}
public class Issue9 {
public static void main(String ... args){
Printer print = new Printer();
Thread t1 = new Thread(new TaskEvenOdd(print, 100, false));
Thread t2 = new Thread(new TaskEvenOdd(print, 100, true));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class TaskEvenOdd implements Runnable {
private int max;
private Printer print;
private boolean isEvenNumber;
TaskEvenOdd(Printer print, int max, boolean isEvenNumber){
this.print = print;
this.max = max;
this.isEvenNumber = isEvenNumber;
}
@Override
public void run() {
int number = isEvenNumber == true ? 2 : 1;
while(number<= max){
if(isEvenNumber){
print.printEven(number);
}
else {
print.printOdd(number);
}
number+=2;
}
}
}
class Printer {
boolean isOdd= false;
synchronized void printEven(int number) {
while(isOdd == false){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Even:"+number);
isOdd = false;
notifyAll();
}
synchronized void printOdd(int number) {
while(isOdd == true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Odd:"+number);
isOdd = true;
notifyAll();
}
}
Exercise9.java
package exercise9;
public class Exercise9 {
static Object lock = new Object();
public static void main(String[] args) {
Thread t1;
t1 = new Thread(() -> {
for (int itr = 10; itr < 101; itr = itr + 2) {
synchronized (lock) {
System.out.println("Even : " + itr);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
//TODO
}
}
}
});
Thread t2;
t2 = new Thread(() -> {
for (int i = 11; i < 101; i = i + 2) {
synchronized (lock) {
System.out.println("Odd : " + i);
try {
lock.notify();
if(i==100)
break;
lock.wait();
} catch (InterruptedException e) {
//TODO
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
} catch (InterruptedException e) {
//TODO
}
}
}
Output :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package issues;
/**
*
* @author abdullah
*/
public class Issue11 {
public static void main(String[] args) {
Thread odd = new Thread(new NumClass(), "Odd Numbers");
Thread even = new Thread(new NumClass(), "Even Numbers");
//Thread th3 = new Thread(new MyClass(), "th3");
// Start first thread immediately
even.start();
/* Start second thread(th2) once first thread(th1)
* is dead
*/
try {
even.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
odd.start();
}
}
class NumClass implements Runnable{
@Override
public void run() {
Thread t = Thread.currentThread();
int countOdd = 0;
int countEven = 0;
System.out.println("Count started: " + t.getName());
try {
Thread.sleep(4000);
for (int i=10; i<101; i++){
if ((i%2==0)&&(t.getName().equalsIgnoreCase("Even Numbers"))){
System.out.print(i+"");
System.out.print((++countOdd % 10 == 0) ? "\n" : " " );
}
if ((i%2==1)&&(t.getName().equalsIgnoreCase("Odd Numbers"))){
System.out.print(i+"");
System.out.print((++countEven % 10 == 0) ? "\n" : " " );
}
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("\nThread ended: "+t.getName() +"\n");
}
}
package com.mycompany.stiw3054rt;
/**
*
* @author USER
*/
public class exercise9 {
static Object lock = new Object();
public static void main(String[] args) {
Thread Odd = new Thread(new Runnable() {
public void run() {
for (int x = 10; x <= 100; x = x + 2){
synchronized (lock) {
System.out.println("Even num: " + x);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread Even = new Thread(new Runnable() {
public void run() {
for (int x = 11; x <= 101; x = x + 2){
synchronized (lock) {
System.out.println("Odd num: " + x);
try {
lock.notify();
if(x==100)
break;
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
Odd.start();
Even.start();
Odd.join();
Even.join();
} catch (Exception e) {
}
}
}
package issue10;
/**
*
* @author esp
*/
public class Issue10 {
public static void main(String[] args) throws InterruptedException {
Issue10 call = new Issue10();
Thread t1 = new Thread(new Runnable() {
public void run() {
call.oddnumber();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
call.evennumber();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
public void oddnumber() {
int todd=0;
for (int k = 11; k <= 100; k = k + 2)
{
System.out.println("Odd is: " + k);
todd = todd +1;
}
System.out.println("Total Odd number is: " + todd);
}
public void evennumber() {
int teven=0;
for (int i = 10; i <= 100; i = i + 2) {
System.out.println("Even is: " + i);
teven = teven + 1;
}
System.out.println("Total Even number is: " + teven);
}
}
package issue10;
public class Issue10 {
static Object lock = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
for (int i = 10; i < 101; i = i + 2) {
synchronized (lock) {
System.out.println(" EVEN " + i);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
for (int i =11; i < 101; i = i + 2) {
synchronized (lock) {
System.out.println(" ODD " + i);
try {
lock.notify();
if(i == 50)
break;
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("\nPrinting over");
} catch (Exception e) {
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.issue10;
/**
*
* @author Alif Haikal
*/
public class Issue10 {
public static void main(String[] args) {
Object lock = new Object();
Issue10 y = new Issue10();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 10; i < 102; i = i + 2) {
System.out.println("Even Number: " + i);
synchronized (lock) {
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int k = 11; k < 100; k = k + 2) {
System.out.println("Odd Number : " + k);
synchronized (lock) {
try {
lock.notify();
if (k == 101) {
break;
}
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
try {
t1.start();
t2.start();
t1.join();
t2.join();
} catch (Exception e) {
System.out.println("Error ");
}
}
}
Write a Java program using TWO (2) threads to count odd number and then even numbers from 10 to 100. (hints: use join keyword)