Эта программа печатает четные и нечетные, используя два потока, с семафором работает нормально. Я попытался использовать wait () и notify (), используя синхронизированный монитор в соответствии с комментарием, так что лучше кодировать без ожидания и уведомлений, но с меньшим количеством межпотоковой связи по сравнению с передачей сообщений с использованием wait и notify.
package com.stocknap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
/*
This program prints even and odd sequence using two thread using samaphore
*/
public class Multithreading {
static Semaphore lck = new Semaphore(1,true);
static volatile AtomicInteger currThd = new AtomicInteger(0);
static class evenThread implements Runnable{
Thread another;
public void setAnother(Thread t){
this.another = t;
}
@Override
public void run() {
while(currThd.get() <= 10){
if (currThd.get() % 2==0 ) {
try {
lck.acquire();
System.out.println("acquiring lock by even thread "+another.getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(currThd);
currThd.addAndGet(1);
System.out.println("value increment by even thd "+another.getName());
lck.release();
System.out.println("lck released by even thd "+another.getName());
}
}
}
}
static class oddThread implements Runnable{
Thread another;
public void setAnother(Thread t){
this.another = t;
}
@Override
public void run() {
while(currThd.get() < 10){
if (currThd.get() % 2!=0 ) {
try {
lck.acquire();
System.out.println("acquiring lock by odd thread "+another.getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(currThd);
currThd.addAndGet(1);
System.out.println("value increment by odd thd "+another.getName());
lck.release();
System.out.println("lck released by odd thd "+another.getName());
}
}
}
}
public static void runFlow (String args[]){
evenThread a = new evenThread();
oddThread b = new oddThread();
Thread t1 = new Thread (a);
Thread t2 = new Thread ( b);
a.setAnother(t2);
b.setAnother(t1);
t1.start();
t2.start();
}
}
работа с wait () и notify ()
/*
This program prints even and odd sequence using two thread with intercommunication of wait and notify
*/
public class MultithreadingCommunication {
static Semaphore lck = new Semaphore(1,true);
static volatile AtomicInteger currThd = new AtomicInteger(0);
static class evenThread implements Runnable{
String msg;
public void setAnother(String t){
this.msg = t;
}
@Override
public void run() {
while(currThd.get() < 10){
synchronized (msg) {
if (currThd.get() % 2 == 0) {
try {
System.out.println("acquiring lock by even thread");
lck.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(currThd);
currThd.addAndGet(1);
System.out.println("value increment by thd");
lck.release();
System.out.println("lck released by thd");
try {
msg.notify();
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
msg.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
static class oddThread implements Runnable{
String msg;
public void setAnother(String t){
this.msg = t;
}
@Override
public void run() {
while(currThd.get() <= 10){
synchronized (msg){
if (currThd.get() % 2!=0 ) {
try {
System.out.println("acquiring lock by odd thread");
lck.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(currThd);
currThd.addAndGet(1);
lck.release();
try {
msg.notify();
}
catch (Exception e){
e.printStackTrace();
}
}
else{
try {
msg.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
public static void runFlow (String args[]){
evenThread a = new evenThread();
oddThread b = new oddThread();
Thread t1 = new Thread (a);
Thread t2 = new Thread ( b);
String msg = "ok";
a.setAnother(msg);
b.setAnother(msg);
t1.start();
t2.start();
}
}