Removed command queue, command is changed via set method, removed time measurement, edited chance to write letter,

This commit is contained in:
WickedJack99
2022-05-23 00:32:05 +02:00
parent 798f32e2a7
commit 0b9b861f22
4 changed files with 35 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
package Controller; package Controller;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Scanner;
import Model.ModelData; import Model.ModelData;
import Model.MyModel; import Model.MyModel;
@@ -9,7 +10,6 @@ import View.MyView;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ConcurrentLinkedQueue<Integer> qCommands = new ConcurrentLinkedQueue<Integer>();
ConcurrentLinkedQueue<ModelData> qDataFromModel = new ConcurrentLinkedQueue<ModelData>(); ConcurrentLinkedQueue<ModelData> qDataFromModel = new ConcurrentLinkedQueue<ModelData>();
Object oThreadLock = new Object(); Object oThreadLock = new Object();
@@ -20,16 +20,29 @@ public class Main {
MyView oView = new MyView(oModelData); MyView oView = new MyView(oModelData);
int iCommand = 2;
oModel.start(); 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. //Wake up model thread if new task to execute.
synchronized(oThreadLock) { synchronized(oThreadLock) {
oThreadLock.notifyAll(); 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.");
} }
} }

View File

@@ -6,4 +6,8 @@ public class ModelData {
public ModelData(String sText) { public ModelData(String sText) {
this.sText = sText; this.sText = sText;
} }
public String getModelString() {
return this.sText;
}
} }

View File

@@ -6,12 +6,9 @@ public class MyModel extends Thread {
private Object oThreadLock; private Object oThreadLock;
private long liStart;
private long liStop;
private ConcurrentLinkedQueue<ModelData> qDataToMain; private ConcurrentLinkedQueue<ModelData> qDataToMain;
private int iModelCommand; private int iModelCommand = 2;
private ModelData oModelData; 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. //Model will be suspended at start until main notifies, that new task has to be executed.
synchronized(oThreadLock) { synchronized(oThreadLock) {
try { try {
liStart = System.nanoTime();
System.out.println("Model suspended"); System.out.println("Model suspended");
oThreadLock.wait(); oThreadLock.wait();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -36,18 +32,20 @@ public class MyModel extends Thread {
e.printStackTrace(); e.printStackTrace();
} }
} }
liStop = System.nanoTime();
System.out.println("Model unsuspended"); System.out.println("Model unsuspended");
System.out.println("Time suspended: " + ((liStop - liStart) / 1000000) + "ms"); System.out.println("Command: " + iModelCommand);
switch (iModelCommand) { switch (iModelCommand) {
case (1): { case (1): {
oModelData = new ModelData(RandomTextCreator.createText(300)); oModelData = new ModelData(RandomTextCreator.createText(300));
qDataToMain.add(oModelData);
}break; }break;
case (2): { case (2): {
}break; }break;
} }
} }
System.out.println("Model-Thread stopped.");
} }
public void setModelCommand(int iModelCommand) { public void setModelCommand(int iModelCommand) {

View File

@@ -6,9 +6,13 @@ public class RandomTextCreator {
int iLastOption = 0; int iLastOption = 0;
for (int i = 0; i < iTextLength; i++) { 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 iMin = 0;
int iMax = 3; 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); int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
while (iOption == iLastOption) { while (iOption == iLastOption) {
iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin); iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
@@ -45,7 +49,7 @@ public class RandomTextCreator {
int iMin = 48; int iMin = 48;
int iMax = 57; int iMax = 57;
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin); int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
return Integer.toString(iOption); return String.valueOf(Character.toChars(iOption));
} }
//65-90 A-Z //65-90 A-Z
@@ -54,7 +58,7 @@ public class RandomTextCreator {
int iMin = 65; int iMin = 65;
int iMax = 90; int iMax = 90;
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin); int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
return Integer.toString(iOption); return String.valueOf(Character.toChars(iOption));
} }
//97-122 a-z //97-122 a-z
@@ -63,7 +67,7 @@ public class RandomTextCreator {
int iMin = 97; int iMin = 97;
int iMax = 122; int iMax = 122;
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin); 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 iMin = 0;
int iMax = 11; int iMax = 11;
int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin); int iOption = (int)(Math.random() * (iMax - iMin + 1) + iMin);
return Integer.toString(aiOptions[iOption]); return String.valueOf(Character.toChars(aiOptions[iOption]));
} }
} }