package Pta;
import java.util.concurrent.Semaphore;
public class Test {
public static void main(String[] args) throws InterruptedException {
Foo foo = new Foo();
Runnable printFirst = new Runnable() {
@Override
public void run() {
System.out.println("first");
}
};
Runnable printSecond = new Runnable() {
@Override
public void run() {
System.out.println("second");
}
};
Runnable printThird = new Runnable() {
@Override
public void run() {
System.out.println("third");
}
};
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
try {
foo.first(printFirst);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
try {
t1.join();
foo.second(printSecond);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t3=new Thread(new Runnable() {
@Override
public void run() {
try {
t2.join();
foo.third(printThird);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t3.start();
t1.start();
t2.start();
}
}
class Foo {
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
}
public void second(Runnable printSecond) throws InterruptedException {
printSecond.run();
}
public void third(Runnable printThird) throws InterruptedException {
printThird.run();
}
}