Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

looks good now

  • Loading branch information...
commit 4362ae133bcae3a3d2d46a2cd88bb90ebc708463 1 parent a367e7e
@palladius authored
Showing with 27 additions and 17 deletions.
  1. +4 −13 src/Exercise.java
  2. +22 −3 src/README.txt
  3. +1 −1  src/Train.java
View
17 src/Exercise.java
@@ -9,10 +9,8 @@
*
*/
-//import java.util.Arrays;
-
public class Exercise {
- public static final String ProgramVersion = "1.0.01alpha" ;
+ public static final String ProgramVersion = "1.0.02" ;
public static void main(String[] args) throws InterruptedException {
// initialization
@@ -21,18 +19,11 @@ public static void main(String[] args) throws InterruptedException {
System.out.println("Exercise.Main(v"+ProgramVersion+"): Starting simulation with "+
Country.NumberOfTrains +" trains and "+ Country.NumberOfStations +" stations");
- CargoManager cargo_thread = new CargoManager();
- cargo_thread.start();
+ CargoManager cargoThread = new CargoManager();
+ cargoThread.start();
Thread visualizatorThread = new Visualizator();
visualizatorThread.start();
-
-// while(true) {
-// try{
-// Country.getInstance().wait();
-// } catch (InterruptedException e) {
-// System.out.println("Exercise.Main: Something woke me up");
-// }
-// }
+
System.out.println("Exercise.Main: exiting main thread"); // it would be nice to gather threads info before quitting
}
}
View
25 src/README.txt
@@ -30,11 +30,30 @@ and their cargo capacity can be easily modified.
The solution is a simple CLI client that might be invoked this way (see Makefile)
- javac *.java
- java Exercise
+ make clean
+ make run
* A station can have multiple trains, a railway is a shared resource with at most ONE train.
* There are 2 threads for visualization and cargo generation, plus Nt where is the number of trains.
As output, the program provides a log-like standard output and a status file with a synoptic which
-helps understand the state of the system.
+helps understand the state of the system. A couple of snapshots have been included in case some problem
+would arise with the code. A VerboseThread abstract class has been created to provide a harmonized logging
+output, wherever possible.
+
+The main() class is Exercise.java and it tries to be completely decoupled from the business logic.
+
+The parameters of the problem have been put all in the Country class (which is our singleton and provides a
+centralized "world" for application variables. I didn't have time to move those parameters to ARGV or to a
+configuration file.
+
+The synchronization among the instances (mostly Trains/Cargos/Stations) have been reached with a few native
+mechanisms of Java (I'm using Java 5):
+
+- synchronized methods (to make sure 2 threads cant access the same method at the same time).
+- synchronized statements (ditto, for a subpart of it)
+- locks on Java Objects (to make sure 2 threads cant access the same object at the same time).
+
+I implemented this exercise using Eclipse.
+
+See the single classes JaveDocs for additional info.
View
2  src/Train.java
@@ -102,7 +102,7 @@ public synchronized void registerToRailway(int railway_number) {
}
/**
- * Status transitions (trivial in this case)
+ * Status transitions (trivial in this case: 1-2-3-4-1-2-3-4-...)
*
* */
public synchronized void set_status(TrainStatus new_status) {
Please sign in to comment.
Something went wrong with that request. Please try again.