hier das komplette Programm aktuell:
[CODE lang="java" title="StartFenster"]import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartFenster {
JFrame cpu = new JFrame();
JPanel pcpu = new JPanel();
JButton[] coregui = new JButton[6];
private JFrame mf = new JFrame("Main");
private JPanel pmain = new JPanel();
private JLabel Ueberschrift = new JLabel("Scheduling auf Multiprozessoren");
private JButton TS = new JButton("Time Sharing");
private JButton SS = new JButton("Space Sharing");
private JButton GS = new JButton("Gang Sharing");
int Szenario=0;
mainFenster mainF;
ExternalThread thread = new ExternalThread();
Core[] core = new Core[6];
StartFenster() throws InterruptedException {
mainF = new mainFenster(this);
cpuFrameerstelle();
Startfenster();
start_cores();
}
public void cpuFrameerstelle() throws InterruptedException {
cpu.setSize(600, 400);
cpu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pcpu.setBorder(new EmptyBorder(60, 60, 60, 60));
pcpu.setLayout(new GridLayout(2,3, 75, 60));
for (int j=0; j<6; j++) {
coregui[j] = new JButton((Integer.toString(j)));
coregui[j].setOpaque(true);
coregui[j].setBackground(Color.white);
coregui[j].setVisible(true);
pcpu.add(coregui[j]);
}
System.out.println("Buttons erstellt");
cpu.add(pcpu);
cpu.setVisible(true);
thread.getSome(this);
thread.start();
}
public void Startfenster(){
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.setSize(800, 800);
mf.setLocationRelativeTo(null);
mf.setVisible(true);
pmain.setLayout(new GridLayout(4, 1, 0, 80));
pmain.setBorder(new EmptyBorder(40, 80, 80, 80));
Ueberschrift.setFont(new Font("Arial", Font.BOLD, 35));
Ueberschrift.setBorder(new EmptyBorder(0, 50, 0, 0));
Font font = new Font("Arial", Font.PLAIN, 20);
TS.setFont(font);
SS.setFont(font);
GS.setFont(font);
// Button Action Listener
TS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hide();
if (Szenario==0)
mainF.build(1);
else if(Szenario==1)
mainF.show();
else {
mainF.change(1);
mainF.show();
}
Szenario=1;
}
});
SS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hide();
if (Szenario==0)
mainF.build(2);
else if(Szenario==2)
mainF.show();
else {
mainF.change(2);
mainF.show();
}
Szenario=2;
}
});
GS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hide();
if (Szenario == 0)
mainF.build(3);
else if (Szenario == 3)
mainF.show();
else{
mainF.change(3);
mainF.show();
}
Szenario = 3;
}
});
pmain.add(Ueberschrift);
pmain.add(TS);
pmain.add(SS);
pmain.add(GS);
mf.add(pmain);
}
//Quatsch
public void changecolorred(int i) {
coregui.setBackground(Color.red);
}
public void changecolorgreen(int i) {
coregui.setBackground(Color.green);
}
public void hide(){
mf.setVisible(false);
cpu.setVisible(true);
}
public void up(){
mf.setVisible(true);
cpu.setVisible(false);
}
public void start_cores(){
for (int i=0; i<6; i++) {
core = new Core();
core.start();
}
}
}
[/CODE]
[CODE lang="java" title="main"]public class Main {
public static void main(String[] args) throws InterruptedException {
new StartFenster();
}
}[/CODE]
[CODE lang="java" title="Core"]import java.util.Timer;
import java.util.TimerTask;
public class Core extends Thread{
int nummer, aufgabe, laenge;
boolean Idle=true;
Core(){
}
public void run(){
while (true) {
while (Idle) {
System.out.println("Idle");
try {
this.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Idle finished");
System.out.println("Core: " + nummer + " Aufgabe: " + aufgabe + " Zeit: " + laenge);
try {
this.sleep(laenge * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Core: " + nummer + " finished.");
Idle=true;
}
}
public void setNum(int nummer, int aufgabe, int laenge){
this.nummer =nummer;
this.aufgabe = aufgabe;
this.laenge = laenge;
}
public void setIdle(){
this.Idle=false;
}
public boolean getIdle(){
return this.Idle;
}
}
[/CODE]
[CODE lang="java" title="ExternalThread"]import java.util.Timer;
import java.util.TimerTask;
public class ExternalThread extends Thread {
StartFenster fenster;
Timer timer = new Timer();
boolean core=false;
int round=0;
ExternalThread(){
}
public void run(){
timer.schedule(new TimerTask() {
@Override
public void run() {
for (int i = 0; i < 6; i++) {
if (!fenster.core.getIdle())
fenster.changecolorred(i);
else
fenster.changecolorgreen(i);
}
}
}, 50, 500);
}
public void getSome(StartFenster fenster){
this.fenster=fenster;
}
public void core_start(){
this.core = true;
}
}
[/CODE]
[CODE lang="java" title="main Fenster"]import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainFenster {
StartFenster sF;
WorstSzenario wS;
private JFrame kf = new JFrame();
private JPanel pkf = new JPanel();
private JLabel Kategorie = new JLabel();
private JLabel Zeit = new JLabel("Gesamtzeit: ");
private JLabel zAngabe = new JLabel("0");
private JLabel Time = new JLabel("Zyklus: 3 Sekunden");
private JButton normalSzenario = new JButton("Normal");
private JButton worstSzenario = new JButton("Worst");
private JButton optimalSzenario = new JButton("Optimal");
private JButton back = new JButton("Back");
GridBagConstraints c = new GridBagConstraints();
private String[][] Tabelle = new String[16][5];
DefaultTableModel model = new DefaultTableModel();
int tE=0;
mainFenster(StartFenster sF) {
this.sF = sF;
wS = new WorstSzenario(this);
}
public void build(int Szenario){
kf.setSize(700, 800);
kf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
kf.setVisible(true);
kf.setLocationRelativeTo(null);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5,5,5,5);
pkf.setLayout(new GridBagLayout());
kf.add(pkf);
Kategorie.setFont(new Font("Arial", Font.BOLD, 35));
change(Szenario);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
c.gridheight = 1;
pkf.add(Kategorie, c);
model.setRowCount(0);
// Die Titel der Spalten setzen
model.setColumnIdentifiers(new Object[]{"Aufgabe", "Zustand", "Core", "Gruppe", "bea. Rechenz."});
build_Tabelle();
Object[] row = new Object[model.getColumnCount()];
// Das Model mit zufälligen Daten befüllen
for (int r = 0; r < 16; r++) {
for (int c = 0; c < row.length; c++) {
row[c] = Tabelle[r][c];
}
model.addRow(row);
}
JTable table1 = new JTable(model);
JScrollPane pane = new JScrollPane(table1);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 2;
c.gridwidth = 4;
pkf.add(pane, c);
Zeit.setFont(new Font("Arial", Font.BOLD, 15));
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=3;
c.gridwidth=1;
c.gridheight=1;
pkf.add(Zeit, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=3;
c.gridwidth=1;
c.gridheight=1;
zAngabe.setFont(new Font("Arial", Font.BOLD, 15));
pkf.add(zAngabe, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=2;
c.gridy=3;
c.gridwidth=2;
c.gridheight=1;
zAngabe.setFont(new Font("Arial", Font.BOLD, 15));
pkf.add(Time, c);
Time.setVisible(false);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=4;
c.gridwidth=1;
c.gridheight=1;
pkf.add(worstSzenario, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=4;
c.gridwidth=1;
c.gridheight=1;
pkf.add(normalSzenario, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=2;
c.gridy=4;
c.gridwidth=1;
c.gridheight=1;
pkf.add(optimalSzenario, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=3;
c.gridy=4;
c.gridwidth=1;
c.gridheight=1;
pkf.add(back, c);
buttonaction();
}
public void buttonaction(){
worstSzenario.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wS.start_Worst();
}
});
normalSzenario.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
optimalSzenario.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Time.setVisible(false);
hide();
sF.up();
}
});
}
public void change(int Szenario){
if(Szenario==1) {
Kategorie.setText("Time Sharing");
kf.setTitle("Time Sharing");
}
else if(Szenario==2) {
Kategorie.setText("Space Sharing");
kf.setTitle("Space Sharing");
}
else {
Kategorie.setText("Gang Sharing");
kf.setTitle("Gang Sharing");
}
}
public void show(){
kf.setVisible(true);
}
public void hide(){
kf.setVisible(false);
}
public void change_Tabelle(int i, int Core, String neu){
Tabelle[1]= "finished";
Tabelle[2] = (Integer.toString(Core));
Tabelle[4] = neu;
model.removeRow(0);
Object[] row = new Object[model.getColumnCount()];
for (int c = 0; c < row.length; c++) {
row[c] = Tabelle[c];
}
model.addRow(row);
}
public void build_Tabelle(){
if(tE==15){
for (int i=tE; i>=1; i--)
model.removeRow(i);
}
for (int i = 0; i<15; i++){
for (int j = 0; j < 5; j++){
if(j==0)
Tabelle[j] = (Integer.toString(i+1));
else if(j==1)
Tabelle[j] = "waiting";
else if(j==2)
Tabelle[j] = "-1";
else
Tabelle[j] = "keine";
}
}
Tabelle[15][0]="Erledigt:";
tE=15;
}
}
[/CODE]
[CODE lang="java" title="worstSzenario"]import com.sun.tools.javac.Main;
public class WorstSzenario {
int Anz=15, round =0;
boolean nonfound=false;
mainFenster mF;
Aufgaben[] ag = new Aufgaben[Anz];
WorstSzenario(mainFenster mF){
this.mF = mF;
}
public void start_Worst(){
// Erstellen der Threads und Aufgaben
create_Aufgaben();
//Den Cores eine Aufgabe zuweisen
while (!nonfound){
for (int i=0; i<6; i++){
if(mF.sF.core.getIdle()) {
int k = searchAufgabe();
if(k==-1){
nonfound=true;
break;
}
mF.sF.core.setNum(i, k, ag[k].getLänge());
mF.change_Tabelle(k, i, (Integer.toString(ag[k].getLänge())));
mF.sF.core.setIdle();
}
}
}
System.out.println("Worst Simulation beendet");
}
public void create_Aufgaben() {
for (int i = 0; i < Anz; i++){
ag = new Aufgaben();
ag.Aufgaben(i);
}
System.out.println("Aufgaben erstellt");
}
// worst search Aufgabe --> lange aufgaben werden bevorzugt kurzen wird keine Beachtung geschenkt, auch andersherum
// möglich
public int searchAufgabe(){
int laenge = 0,j = -1;
for (int i=0; i<Anz; i++) {
if (!ag.getbearbeitet() && ag.getLänge() > laenge) {
j=i;
laenge=ag.getLänge();
}
}
if(j!=-1)
ag[j].setBearbeitet();
return j;
}
}
[/CODE]
[CODE lang="java" title="aufgaben"]import java.util.Random;
public class Aufgaben {
private int Nummer;
private int Länge;
private boolean bearbeitet;
public void Aufgaben(int Nummer){
this.Nummer=Nummer;
this.Länge = random();
this.bearbeitet = false;
}
public int random(){
Random ran = new Random();
int x = (ran.nextInt(10)+5);
return x;
}
public int getLänge(){
return this.Länge;
}
public boolean getbearbeitet(){return this.bearbeitet;}
public void setBearbeitet(){this.bearbeitet=true;}
}
[/CODE]