Created
May 4, 2016 19:56
-
-
Save AnnaBoro/29dbdbb2b6ddf2ecc78c2cdcc7710e56 to your computer and use it in GitHub Desktop.
wait/notify + new car in cycle
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 = 0; | |
private int y = 160; | |
private Color color; | |
public void setStep() { | |
this.step = step + 1; | |
} | |
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 < 400; 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() { | |
while (true) { | |
moveCar(); | |
car = new Car(); | |
} | |
} | |
}).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() { | |
while (true) { | |
try { | |
synchronized (garage) { | |
System.out.println("garage wait"); | |
garage.wait(); | |
System.out.println("garage go"); | |
} | |
animateGarage(); | |
synchronized (car) { | |
car.notify(); | |
} | |
synchronized (garage) { | |
garage.wait(); | |
garage.closeGarage(); | |
sleep(100); | |
System.out.println("close garage"); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}).start(); | |
} | |
public void moveCar() { | |
car.move(); | |
while (car.getX() <= WIDTH - 20) { | |
if (car.getX() == (garage.getX() + 40)) { | |
synchronized (garage) { | |
car.setStep(); | |
garage.notify(); | |
System.out.println("car " + (garage.getX() + 40)); | |
} | |
} | |
else if (car.getX() == (garage.getX() - 30)) { | |
car.stop(); | |
synchronized (garage) { | |
car.setStep(); | |
garage.notify(); | |
System.out.println("car " + (garage.getX() - 30)); | |
} | |
try { | |
synchronized (car) { | |
car.wait(); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
car.move(); | |
// System.out.println("qqqqqq"); | |
} | |
sleep(5); | |
} | |
System.out.println("new car"); | |
} | |
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