Created
April 30, 2016 18:44
-
-
Save AnnaBoro/94b4817cf33c77c54ad3ecb77732bda2 to your computer and use it in GitHub Desktop.
wait/notify + 1 more thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson9.waitnotify; | |
import java.awt.*; | |
public class Car { | |
private int x = 10; | |
private int y = 160; | |
private Color color; | |
private int step = 0; | |
private boolean isMoving = false; | |
public Car(){ | |
color = Color.RED; | |
} | |
public void draw(Graphics g) { | |
g.setColor(this.color); | |
g.fillRect(x + step, y, 20, 10); | |
} | |
public void move() { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
isMoving = false; | |
for (int i = step; i < 360; i++) { | |
if (isMoving) { | |
break; | |
} | |
step++; | |
try { | |
Thread.sleep(50); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}).start(); | |
} | |
public int getX() { | |
return x + step; | |
} | |
public int getY() { | |
return y; | |
} | |
public void stop() { | |
isMoving = true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson9.waitnotify; | |
import java.awt.*; | |
public class Garage{ | |
private int x = 200; | |
private int y = 100; | |
private Color color; | |
private int step = 0; | |
private boolean isOpen = false; | |
public Garage(){ | |
color = Color.cyan; | |
} | |
public void draw(Graphics g) { | |
g.setColor(this.color); | |
g.fillRect(x, y - step, 40, 60); | |
g.fillRect(x, y + 60 + step, 40, 60); | |
} | |
public void openGarage() { | |
for (int i = 0; i < 50; i++) { | |
step++; | |
try { | |
Thread.sleep(40); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
isOpen = true; | |
} | |
public void closeGarage() { | |
for (int i = 0; i < 50; i++) { | |
step--; | |
try { | |
Thread.sleep(40); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
isOpen = false; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y - step; | |
} | |
public boolean isOpen() { | |
return isOpen; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson9.waitnotify; | |
import javax.swing.*; | |
import java.awt.*; | |
public class ParkingToGarage extends JPanel { | |
private static final int WIDTH = 400; | |
private Garage garage; | |
private Car car; | |
public static void main(String[] args) { | |
new ParkingToGarage(); | |
} | |
public ParkingToGarage() { | |
JFrame jFrame = new JFrame("Garage"); | |
jFrame.setLocation(450, 150); | |
jFrame.setMinimumSize(new Dimension(WIDTH, 400)); | |
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
jFrame.getContentPane().add(this); | |
jFrame.pack(); | |
jFrame.setVisible(true); | |
garage = new Garage(); | |
car = new Car(); | |
turnOnGarage(); | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
moveCar(); | |
} | |
}).start(); | |
while (true) { | |
repaint(); | |
sleep(1000 / 60); | |
} | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
garage.draw(g); | |
car.draw(g); | |
} | |
public void turnOnGarage() { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
synchronized (garage) { | |
garage.wait(); | |
} | |
while (!garage.isOpen()) { | |
animateGarage(); | |
} | |
synchronized (car) { | |
car.notify(); | |
} | |
synchronized (garage) { | |
garage.wait(); | |
garage.closeGarage(); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
public void moveCar() { | |
car.move(); | |
while (car.getX() <= WIDTH - 20) { | |
if (car.getX() == 240) { | |
synchronized (garage) { | |
garage.notify(); | |
} | |
} | |
else if (car.getX() == 170) { | |
car.stop(); | |
synchronized (garage) { | |
garage.notify(); | |
} | |
try { | |
synchronized (car) { | |
car.wait(); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
car.move(); | |
} | |
sleep(5); | |
} | |
} | |
public void animateGarage() { | |
if (!garage.isOpen()) { | |
if (garage.getY() > 50) { | |
garage.openGarage(); | |
} | |
} else { | |
garage.closeGarage(); | |
} | |
} | |
private void sleep(int timeout) { | |
try { | |
Thread.sleep(timeout); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment