yeet
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
package P7;
|
package P7;
|
||||||
|
|
||||||
public class Gabel {
|
public class Fork {
|
||||||
public boolean takable =true;
|
public boolean takable =true;
|
||||||
int id;
|
int id;
|
||||||
public Gabel(int pID){
|
public Fork(int pID){
|
||||||
id=pID;
|
id=pID;
|
||||||
}
|
}
|
||||||
public synchronized boolean take(String name) {
|
public synchronized boolean take(String name) {
|
||||||
@@ -17,5 +17,4 @@ public class Gabel {
|
|||||||
System.out.println(name+" hat Gabel "+id+" zuruckgelegt");
|
System.out.println(name+" hat Gabel "+id+" zuruckgelegt");
|
||||||
takable = true;
|
takable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,8 @@ package P7;
|
|||||||
|
|
||||||
public class Philosoph extends Thread {
|
public class Philosoph extends Thread {
|
||||||
String name;
|
String name;
|
||||||
Gabel left,right;
|
Fork left,right;
|
||||||
public Philosoph(String pname,Gabel pleft, Gabel pright){
|
public Philosoph(String pname, Fork pleft, Fork pright){
|
||||||
name=pname;
|
name=pname;
|
||||||
left=pleft;
|
left=pleft;
|
||||||
right=pright;
|
right=pright;
|
||||||
@@ -21,12 +21,14 @@ public class Philosoph extends Thread {
|
|||||||
if(right.take(name)){
|
if(right.take(name)){
|
||||||
if(left.take(name)){
|
if(left.take(name)){
|
||||||
System.out.println(name+" " +"Yeeting");
|
System.out.println(name+" " +"Yeeting");
|
||||||
|
right.place(name);
|
||||||
|
left.place(name);
|
||||||
}else{
|
}else{
|
||||||
right.place(name);
|
right.place(name);
|
||||||
System.out.println("kein links");
|
System.out.println(name+" hat kein links");
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
System.out.println("kein rechts");
|
System.out.println(name+" hat kein rechts");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ public class Philosophen {
|
|||||||
//int n =Integer.parseInt(args[0]);
|
//int n =Integer.parseInt(args[0]);
|
||||||
int n = 5;
|
int n = 5;
|
||||||
var pList = new ArrayList<Thread>();
|
var pList = new ArrayList<Thread>();
|
||||||
var gList= new ArrayList<Gabel>();
|
var gList= new ArrayList<Fork>();
|
||||||
for(int it =0;it<n;it++){
|
for(int it =0;it<n;it++){
|
||||||
Gabel g=new Gabel(it);
|
Fork g=new Fork(it);
|
||||||
gList.add(g);
|
gList.add(g);
|
||||||
}
|
}
|
||||||
for(int i=0;i<n;i++){
|
for(int i=0;i<n;i++){
|
||||||
Gabel left = gList.get(i);
|
Fork left = gList.get(i);
|
||||||
Gabel right;
|
Fork right;
|
||||||
if (i==0){
|
if (i==0){
|
||||||
right = gList.get(gList.size()-1);
|
right = gList.get(gList.size()-1);
|
||||||
}else {
|
}else {
|
||||||
@@ -24,7 +24,5 @@ public class Philosophen {
|
|||||||
pList.add(t);
|
pList.add(t);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package PhilosophenPak;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class PhilosophenAIO {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//int n =Integer.parseInt(args[0]);
|
||||||
|
int n = 5;
|
||||||
|
var pList = new ArrayList<Thread>();
|
||||||
|
var gList= new ArrayList<Gabel>();
|
||||||
|
for(int it =0;it<n;it++){
|
||||||
|
Gabel g=new Gabel(it);
|
||||||
|
gList.add(g);
|
||||||
|
}
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
Gabel left = gList.get(i);
|
||||||
|
Gabel right;
|
||||||
|
if (i==0){
|
||||||
|
right = gList.get(gList.size()-1);
|
||||||
|
}else {
|
||||||
|
right = gList.get(i-1);
|
||||||
|
}
|
||||||
|
Thread t = new Philosoph("Philosoph "+i,left, right);
|
||||||
|
pList.add(t);
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Philosoph extends Thread {
|
||||||
|
String name;
|
||||||
|
Gabel left,right;
|
||||||
|
public Philosoph(String pname,Gabel pleft, Gabel pright){
|
||||||
|
name=pname;
|
||||||
|
left=pleft;
|
||||||
|
right=pright;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true){
|
||||||
|
try {
|
||||||
|
Thread.sleep(500);
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if(right.take(name)){
|
||||||
|
if(left.take(name)){
|
||||||
|
System.out.println(name+" " +"Yeeting");
|
||||||
|
right.place(name);
|
||||||
|
left.place(name);
|
||||||
|
}else{
|
||||||
|
right.place(name);
|
||||||
|
System.out.println(name+" hat kein links");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
System.out.println(name+" hat kein rechts");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Gabel {
|
||||||
|
public boolean takable =true;
|
||||||
|
int id;
|
||||||
|
public Gabel(int pID){
|
||||||
|
id=pID;
|
||||||
|
}
|
||||||
|
public synchronized boolean take(String name) {
|
||||||
|
if(!takable)
|
||||||
|
return false;
|
||||||
|
System.out.println(name+" hat Gabel "+id+" genommen");
|
||||||
|
takable=false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public synchronized void place(String name){
|
||||||
|
System.out.println(name+" hat Gabel "+id+" zuruckgelegt");
|
||||||
|
takable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,46 +49,35 @@ void aufgabe4(){
|
|||||||
|
|
||||||
#define ARRAY_SIZE 5
|
#define ARRAY_SIZE 5
|
||||||
|
|
||||||
// Funktion zur Initialisierung des Arrays
|
|
||||||
void initializeArray(int arr[]) {
|
void initializeArray(int arr[]) {
|
||||||
printf("Geben Sie %d Ganzzahlen für das Array ein:\n", ARRAY_SIZE);
|
printf("Geben Sie %d Ganzzahlen für das Array ein:\n", ARRAY_SIZE);
|
||||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||||
scanf("%d", &arr[i]);
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Funktion zum Ausdrucken des Arrays
|
|
||||||
void printArray(int arr[]) {
|
void printArray(int arr[]) {
|
||||||
printf("Das Array lautet: ");
|
printf("Das Array lautet: ");
|
||||||
|
int *ptr = arr;
|
||||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", *(ptr + i));
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
int sumArray(const int arr[]) {
|
||||||
// Funktion zur Berechnung der Summe des Arrays
|
|
||||||
int sumArray(int arr[]) {
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int i = 0; i < ARRAY_SIZE; i++) {
|
for (int i = 0; i < ARRAY_SIZE; i++) {
|
||||||
sum += *(arr + i); // Pointer-Arithmetik: äquivalent zu arr[i]
|
sum += *(arr + i);
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
int aufgabe5() {
|
void aufgabe5() {
|
||||||
int arr[ARRAY_SIZE];
|
int arr[ARRAY_SIZE];
|
||||||
|
|
||||||
// Array initialisieren
|
|
||||||
initializeArray(arr);
|
initializeArray(arr);
|
||||||
|
|
||||||
// Array ausgeben
|
|
||||||
printArray(arr);
|
printArray(arr);
|
||||||
|
|
||||||
// Summe berechnen und ausgeben
|
|
||||||
int sum = sumArray(arr);
|
int sum = sumArray(arr);
|
||||||
printf("Die Summe der Elemente im Array beträgt: %d\n", sum);
|
printf("Die Summe der Elemente im Array beträgt: %d\n", sum);
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
out/production/Anwendungsentwicklung/PhilosophenPak/Gabel.class
Normal file
BIN
out/production/Anwendungsentwicklung/PhilosophenPak/Gabel.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user