public class myservice {
private volatile int ordernum = 1;
public synchronized void methoda() {
try {
while (ordernum != 1) {
wait();
}
for (int i = 0; i < 2; i++) {
system.out.println("aaaaa");
}
ordernum = 2;
notifyall();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
public synchronized void methodb() {
try {
while (ordernum != 2) {
wait();
}
for (int i = 0; i < 2; i++) {
system.out.println("bbbbb");
}
ordernum = 3;
notifyall();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
public synchronized void methodc() {
try {
while (ordernum != 3) {
wait();
}
for (int i = 0; i < 2; i++) {
system.out.println("ccccc");
}
ordernum = 1;
notifyall();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
import service.myservice;
public class threadaa extends thread {
private myservice dbtools;
public threadaa(myservice dbtools) {
super();
this.dbtools = dbtools;
}
@override
public void run() {
dbtools.methoda();
}
}
import service.myservice;
public class threadbb extends thread {
private myservice dbtools;
public threadbb(myservice dbtools) {
super();
this.dbtools = dbtools;
}
@override
public void run() {
dbtools.methodb();
}
}
import service.myservice;
public class threadcc extends thread {
private myservice dbtools;
public threadcc(myservice dbtools) {
this.dbtools = dbtools;
}
@override
public void run() {
dbtools.methodc();
}
}
import extthread.threadcc;
import service.myservice;
import extthread.threadaa;
import extthread.threadbb;
public class run {
public static void main(string[] args) {
myservice myservice = new myservice();
for (int i = 0; i < 2; i++) {
threadbb output = new threadbb(myservice);
output.start();
threadaa input = new threadaa(myservice);
input.start();
threadcc threadcc = new threadcc(myservice);
threadcc.start();
}
}
}