Removed command queue, command is changed via set method, removed time measurement, edited chance to write letter,
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package Controller;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.Scanner;
|
||||
|
||||
import Model.ModelData;
|
||||
import Model.MyModel;
|
||||
@@ -9,7 +10,6 @@ import View.MyView;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
ConcurrentLinkedQueue<Integer> qCommands = new ConcurrentLinkedQueue<Integer>();
|
||||
ConcurrentLinkedQueue<ModelData> qDataFromModel = new ConcurrentLinkedQueue<ModelData>();
|
||||
|
||||
Object oThreadLock = new Object();
|
||||
@@ -20,16 +20,29 @@ public class Main {
|
||||
|
||||
MyView oView = new MyView(oModelData);
|
||||
|
||||
|
||||
int iCommand = 2;
|
||||
|
||||
oModel.start();
|
||||
|
||||
while (true) {
|
||||
while (iCommand != 0) {
|
||||
Scanner oScanner = new Scanner(System.in);
|
||||
System.out.print("Enter command: ");
|
||||
iCommand = oScanner.nextInt(); //TODO remove statement if gui finished
|
||||
|
||||
oModel.setModelCommand(iCommand);
|
||||
//Wake up model thread if new task to execute.
|
||||
synchronized(oThreadLock) {
|
||||
oThreadLock.notifyAll();
|
||||
}
|
||||
|
||||
while (qDataFromModel.isEmpty() && (iCommand == 1)) {
|
||||
//Wait for model to finish TODO remove loop if gui is finished
|
||||
}
|
||||
if (!qDataFromModel.isEmpty()) {
|
||||
oModelData = qDataFromModel.poll();
|
||||
System.out.println("Data received from Model:\n" + oModelData.getModelString());
|
||||
}
|
||||
}
|
||||
System.out.println("Main-Thread stopped.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,8 @@ public class ModelData {
|
||||
public ModelData(String sText) {
|
||||
this.sText = sText;
|
||||
}
|
||||
|
||||
public String getModelString() {
|
||||
return this.sText;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,9 @@ public class MyModel extends Thread {
|
||||
|
||||
private Object oThreadLock;
|
||||
|
||||
private long liStart;
|
||||
private long liStop;
|
||||
|
||||
private ConcurrentLinkedQueue<ModelData> qDataToMain;
|
||||
|
||||
private int iModelCommand;
|
||||
private int iModelCommand = 2;
|
||||
|
||||
private ModelData oModelData;
|
||||
|
||||
@@ -28,7 +25,6 @@ public class MyModel extends Thread {
|
||||
//Model will be suspended at start until main notifies, that new task has to be executed.
|
||||
synchronized(oThreadLock) {
|
||||
try {
|
||||
liStart = System.nanoTime();
|
||||
System.out.println("Model suspended");
|
||||
oThreadLock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
@@ -36,18 +32,20 @@ public class MyModel extends Thread {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
liStop = System.nanoTime();
|
||||
System.out.println("Model unsuspended");
|
||||
System.out.println("Time suspended: " + ((liStop - liStart) / 1000000) + "ms");
|
||||
System.out.println("Command: " + iModelCommand);
|
||||
switch (iModelCommand) {
|
||||
case (1): {
|
||||
oModelData = new ModelData(RandomTextCreator.createText(300));
|
||||
qDataToMain.add(oModelData);
|
||||
}break;
|
||||
case (2): {
|
||||
|
||||
}break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Model-Thread stopped.");
|
||||
}
|
||||
|
||||
public void setModelCommand(int iModelCommand) {
|
||||
|
||||
@@ -6,9 +6,13 @@ public class RandomTextCreator {
|
||||
int iLastOption = 0;
|
||||
|
||||
for (int i = 0; i < iTextLength; i++) {
|
||||
//Formula to calculate random value between max and min. Math.random() * (max - min + 1) + min
|
||||
int iMin = 0;
|
||||
int iMax = 3;
|
||||
if (i % 2 == 0) {
|
||||
iMin = 1;
|
||||
iMax = 2;
|
||||
}
|
||||
//Formula to calculate random value between max and min. Math.random() * (max - min + 1) + min
|
||||
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
|
||||
while (iOption == iLastOption) {
|
||||
iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
|
||||
@@ -45,7 +49,7 @@ public class RandomTextCreator {
|
||||
int iMin = 48;
|
||||
int iMax = 57;
|
||||
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
|
||||
return Integer.toString(iOption);
|
||||
return String.valueOf(Character.toChars(iOption));
|
||||
}
|
||||
|
||||
//65-90 A-Z
|
||||
@@ -54,7 +58,7 @@ public class RandomTextCreator {
|
||||
int iMin = 65;
|
||||
int iMax = 90;
|
||||
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
|
||||
return Integer.toString(iOption);
|
||||
return String.valueOf(Character.toChars(iOption));
|
||||
}
|
||||
|
||||
//97-122 a-z
|
||||
@@ -63,7 +67,7 @@ public class RandomTextCreator {
|
||||
int iMin = 97;
|
||||
int iMax = 122;
|
||||
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
|
||||
return Integer.toString(iOption);
|
||||
return String.valueOf(Character.toChars(iOption));
|
||||
}
|
||||
|
||||
//?!()=+,-[]{}
|
||||
@@ -74,6 +78,6 @@ public class RandomTextCreator {
|
||||
int iMin = 0;
|
||||
int iMax = 11;
|
||||
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
|
||||
return Integer.toString(aiOptions[iOption]);
|
||||
return String.valueOf(Character.toChars(aiOptions[iOption]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user