Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions practice-2-sample/src/main/java/practice2/tiktak/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,42 @@ public int getState() {
return state;
}

public void Tic() {
public synchronized void Tic() {
while (state != 1) {
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("Tic-");
state = 2;
notifyAll();
}

public void Tak() {
System.out.println("Tak");
public synchronized void Tak() {
while (state != 2) {
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("Tak-");
state = 3;
notifyAll();
}

public synchronized void Toy() {
while (state != 3) {
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("Toy");
state = 1;
notifyAll();
}
}
5 changes: 4 additions & 1 deletion practice-2-sample/src/main/java/practice2/tiktak/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ public static void main(String[] args) throws InterruptedException {

Thread w1 = new Thread(new Worker(1, d));
Thread w2 = new Thread(new Worker(2, d));
Thread w3 = new Thread(new Worker(3, d));


w1.start();
w2.start();
w3.start();

w2.join();
w3.join();
System.out.println("end of main...");
}
}
4 changes: 3 additions & 1 deletion practice-2-sample/src/main/java/practice2/tiktak/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public void run() {
for (int i = 0; i < 5; i++) {
if (id == 1) {
data.Tic();
} else {
} else if (id == 2){
data.Tak();
} else if (id == 3){
data.Toy();
}
}
}
Expand Down