java HW 7.1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java HW 7.1相关的知识,希望对你有一定的参考价值。
package com.gmail.vhrushyn;
public class Ship implements Runnable {
int staff;
Port port;
String name;
public Ship() {
super();
staff = 10;
}
public Ship(Port port, String name) {
super();
this.port = port;
this.staff = 10;
this.name = name;
System.out.println("Ship " + name + " created");
}
public int getStaff() {
return staff;
}
public void setStaff(int staff) {
this.staff = staff;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private void unload() {
Dock dock = port.getDock();
// if(!dock.isAvailable()) {
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
System.out.println(name + " going to " + dock.getName());
dock.setAvailable(false);
System.out.println(name + " started unloading to " + dock.getName()+" ...");
int loadedStaff = staff;
for (int i = 0; i < staff; i++) {
loadedStaff -= 1;
dock.setStaff(dock.getStaff() + 1);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("[=]");
}
System.out.println(name + " is unloaded to " + dock.getName());
System.out.println(dock.getName()+" is free now");
staff = loadedStaff;
dock.setAvailable(true);
}
@Override
public void run() {
unload();
}
}
package com.gmail.vhrushyn;
public class Port {
Dock dockOne;
Dock dockTwo;
public Port() {
super();
dockOne = new Dock("First dock");
dockTwo = new Dock("Second dock");
}
public synchronized Dock getDock() {
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
if (dockOne.isAvailable() || dockTwo.isAvailable()) {
return chooseDock();
} else {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Dock chooseDock() {
Dock dock = null;
if (dockOne.isAvailable()) {
dock = dockOne;
return dock;
} else if (dockTwo.isAvailable()) {
dock = dockTwo;
}
return dock;
}
}
package com.gmail.vhrushyn;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Port port = new Port();
Thread tOne = new Thread(new Ship(port, "Calipso"));
Thread tTwo = new Thread(new Ship(port, "Argo"));
Thread tThree = new Thread(new Ship(port, "Duncan"));
// Thread tFour = new Thread(new Ship(port, "Kyiv"));
// Thread tFive = new Thread(new Ship(port, "Odesa"));
tOne.start();
tTwo.start();
tThree.start();
// tFour.start();
// tFive.start();
try {
tOne.join();
tTwo.join();
tThree.join();
// tFour.join();
// tFive.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(port.dockOne.getStaff()+" staff in "+port.dockOne.getName());
System.out.println(port.dockTwo.getStaff()+" staff in "+port.dockTwo.getName());
}
}
package com.gmail.vhrushyn;
public class Dock {
private int staff;
private boolean available;
private String name;
public Dock() {
super();
this.staff = 0;
this.available = true;
}
public Dock(String name) {
super();
this.name = name;
this.staff = 0;
this.available = true;
System.out.println(name+" created");
}
public int getStaff() {
return staff;
}
public void setStaff(int staff) {
this.staff = staff;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以上是关于java HW 7.1的主要内容,如果未能解决你的问题,请参考以下文章