Compare commits
5 Commits
591f0d33d7
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
ab5e888ea8 | ||
|
2a5ddf81bd | ||
|
0c43ff5b3e | ||
|
c653aac568 | ||
|
9688d45433 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -30,3 +30,4 @@ bin/
|
|||||||
|
|
||||||
/gradle/
|
/gradle/
|
||||||
/.gradle/
|
/.gradle/
|
||||||
|
/build/
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
|
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||||
<component name="GradleSettings">
|
<component name="GradleSettings">
|
||||||
<option name="linkedExternalProjectsSettings">
|
<option name="linkedExternalProjectsSettings">
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
|
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@@ -1,4 +1,3 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
<component name="FrameworkDetectionExcludesConfiguration">
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
|
BIN
Folien/Paralleles Programmieren VL1-3.pdf
Normal file
BIN
Folien/Paralleles Programmieren VL1-3.pdf
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
42
src/main/java/Main.java
Normal file
42
src/main/java/Main.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import de.dhbw.parprog.JavaPipe;
|
||||||
|
import smartBank.TransferSpam;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
JavaPipe.PipeMain();
|
||||||
|
//TransferSpam.main();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RecursiveMath{
|
||||||
|
public static int add(int a, int b){
|
||||||
|
if(b < 1){
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
return add(a+1, b-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int multiply(int a, int b) {
|
||||||
|
if (b < 0) {
|
||||||
|
return -multiply(a, -b);
|
||||||
|
} else if (b == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return add(a, multiply(a, b - 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int expo(int a, int b){
|
||||||
|
if (b==0){
|
||||||
|
return 1;
|
||||||
|
}else if (b == 1) {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
return multiply(a, expo(a, b-1));
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(expo(2,6));
|
||||||
|
}
|
||||||
|
}
|
42
src/main/java/Threadpool/ThreadPool.java
Normal file
42
src/main/java/Threadpool/ThreadPool.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package Threadpool;
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import static java.util.concurrent.Executors.newFixedThreadPool;
|
||||||
|
|
||||||
|
public class ThreadPool {
|
||||||
|
private class Calculation implements Callable{
|
||||||
|
@Override
|
||||||
|
public Object call() throws Exception {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public int doCalculation() throws ExecutionException, InterruptedException {
|
||||||
|
int totalResult = 0;
|
||||||
|
int threadPoolSize = 5;
|
||||||
|
int numberOfCalculations = 10;
|
||||||
|
|
||||||
|
Vector<Future<Integer>> allResults = new Vector<>();
|
||||||
|
ExecutorService threadpool = newFixedThreadPool(threadPoolSize);
|
||||||
|
|
||||||
|
for (int i = 0; i < numberOfCalculations; i++) {
|
||||||
|
Future<Integer> oneResult = threadpool.submit(new Calculation());
|
||||||
|
allResults.add(oneResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Future<Integer> result : allResults) {
|
||||||
|
totalResult += result.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
threadpool.shutdown();
|
||||||
|
return totalResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
ThreadPool test = new ThreadPool();
|
||||||
|
System.out.println("Calculation started");
|
||||||
|
int result = test.doCalculation();
|
||||||
|
System.out.println("Result: " + result);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,15 +2,10 @@ package de.dhbw.parprog;
|
|||||||
|
|
||||||
import de.dhbw.parprog.processemu.Pipe;
|
import de.dhbw.parprog.processemu.Pipe;
|
||||||
import de.dhbw.parprog.processemu.ProcessEmu;
|
import de.dhbw.parprog.processemu.ProcessEmu;
|
||||||
import org.apache.commons.lang.NotImplementedException;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.AccessMode;
|
|
||||||
import java.sql.SQLOutput;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
|
|
||||||
public class JavaPipe {
|
public class JavaPipe {
|
||||||
@@ -19,7 +14,7 @@ public class JavaPipe {
|
|||||||
BufferedReader in = new BufferedReader(new InputStreamReader(inputstream));
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputstream));
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void PipeMain() throws IOException {
|
||||||
int Amount = 42;
|
int Amount = 42;
|
||||||
int[] allResults = new int[Amount];
|
int[] allResults = new int[Amount];
|
||||||
Pipe[] pipes = new Pipe[Amount];
|
Pipe[] pipes = new Pipe[Amount];
|
||||||
@@ -36,11 +31,16 @@ public class JavaPipe {
|
|||||||
System.out.println("sup nerd");
|
System.out.println("sup nerd");
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for(int i = 0; i<allResults.length; i++){
|
for (int Result : allResults) {
|
||||||
sum += allResults[i];
|
sum += Result;
|
||||||
System.out.println("Got a return of " + allResults[i]);
|
System.out.println("Got a return of " + Result);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("The sum is " + sum);
|
System.out.println("The sum is " + sum);
|
||||||
|
|
||||||
|
/*
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(pipes[0].getIn()));
|
||||||
|
String result = reader.readLine();
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
72
src/main/java/smartBank/Account.java
Normal file
72
src/main/java/smartBank/Account.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package smartBank;
|
||||||
|
|
||||||
|
|
||||||
|
public class Account {
|
||||||
|
public static long LOWER_LIMIT = 0;
|
||||||
|
public static long UPPER_LIMIT = 100000;
|
||||||
|
|
||||||
|
private long balance;
|
||||||
|
private final long accountNumber;
|
||||||
|
|
||||||
|
public Account(long AccountNumber){
|
||||||
|
this(AccountNumber, 0);
|
||||||
|
}
|
||||||
|
public Account(long Accountnumber, long initialBalance){
|
||||||
|
this.balance = initialBalance;
|
||||||
|
this.accountNumber = Accountnumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public long getBalance() {
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBalance(long balance) {
|
||||||
|
if (balance < LOWER_LIMIT){
|
||||||
|
throw new IllegalAccountStateException();
|
||||||
|
}
|
||||||
|
if (balance > UPPER_LIMIT){
|
||||||
|
throw new IllegalAccountStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.balance = balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void withdraw(long amount) throws IllegalAccountStateException{
|
||||||
|
if (amount > this.balance){
|
||||||
|
throw new IllegalAccountStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setBalance( (this.getBalance() - amount) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deposit(long amount) throws IllegalAccountStateException{
|
||||||
|
if((this.getBalance()+amount) > UPPER_LIMIT){
|
||||||
|
throw new IllegalAccountStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setBalance( (this.getBalance()+amount) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Account getLowerIdAccount(Account a1, Account a2){
|
||||||
|
if ((a1 != a2) && (a1.accountNumber == a2.accountNumber)){
|
||||||
|
throw new RuntimeException("2 Accounts with Same ID");
|
||||||
|
}
|
||||||
|
if (a1 == a2){
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a1.accountNumber < a2.accountNumber){
|
||||||
|
return a1;
|
||||||
|
}
|
||||||
|
return a2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Account getHigherIdAccount(Account a1, Account a2){
|
||||||
|
if (getLowerIdAccount(a1, a2) == a1){
|
||||||
|
return a2;
|
||||||
|
}else {
|
||||||
|
return a1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
82
src/main/java/smartBank/Bank.java
Normal file
82
src/main/java/smartBank/Bank.java
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package smartBank;
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
|
||||||
|
public class Bank {
|
||||||
|
static long AccountsInUse = 0;
|
||||||
|
public Vector<Account> allAccounts = new Vector<Account>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erzeugt ein neues Konto mit initialem Kontostand 0.
|
||||||
|
* @return das neue Konto
|
||||||
|
*/
|
||||||
|
public synchronized Account createAccount() {
|
||||||
|
Account newAccount = new Account(AccountsInUse++);
|
||||||
|
allAccounts.add(newAccount);
|
||||||
|
return newAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAllBalances(long value){
|
||||||
|
for (Account account : this.allAccounts) {
|
||||||
|
account.setBalance(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ruft den aktuellen Kontostand ab
|
||||||
|
* @param account Konto, dessen Kontostand bestimmt werden soll
|
||||||
|
* @return der aktuelle Kontostand
|
||||||
|
* @throws IllegalArgumentException bei ungültigen Parametern
|
||||||
|
*/
|
||||||
|
public long getBalance(Account account) throws IllegalArgumentException {
|
||||||
|
return account.getBalance();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Einzahlen eines bestimmten Betrags
|
||||||
|
* @param account das Konto, auf den der Betrag eingezahlt werden soll
|
||||||
|
* @param amount der Betrag (muß >=0 sein)
|
||||||
|
* @throws IllegalArgumentException bei ungültigen Parametern
|
||||||
|
* @throws IllegalAccountStateException falls der Kontostand außerhalb des gültigen Wertebereichs geraten würde
|
||||||
|
*/
|
||||||
|
public void deposit(Account account, long amount) throws IllegalAccountStateException, IllegalArgumentException {
|
||||||
|
account.deposit(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abheben eines bestimmten Betrags
|
||||||
|
* @param account das Konto, von dem der Betrag abgehoben werden soll
|
||||||
|
* @param amount der Betrag (muß >=0 sein)
|
||||||
|
* @throws IllegalArgumentException bei ungültigen Parametern
|
||||||
|
* @throws IllegalAccountStateException falls der Kontostand außerhalb des gültigen Wertebereichs geraten würde
|
||||||
|
*/
|
||||||
|
public void withdraw(Account account, long amount) throws IllegalAccountStateException, IllegalArgumentException {
|
||||||
|
account.withdraw(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Überweisen eines Betrags von einem Konto auf ein anderes
|
||||||
|
* @param fromAccount Konto, von dem abgebucht werden soll
|
||||||
|
* @param toAccount Konto, auf das gutgeschrieben werden soll
|
||||||
|
* @param amount der zu transferierende Betrag (muß >=0 sein)
|
||||||
|
* @throws IllegalArgumentException bei ungültigen Parametern
|
||||||
|
* @throws IllegalAccountStateException falls einer der Kontostände außerhalb des gültigen Wertebereichs geraten würde
|
||||||
|
*/
|
||||||
|
public void transfer(Account fromAccount, Account toAccount, long amount) throws IllegalAccountStateException, IllegalArgumentException {
|
||||||
|
fromAccount.withdraw(amount);
|
||||||
|
toAccount.deposit(amount);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H"};
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
for (int i = 0; i < this.allAccounts.size(); i++) {
|
||||||
|
result += (alphabet[i] + " " + allAccounts.get(i).getBalance() + " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,5 @@
|
|||||||
|
package smartBank;
|
||||||
|
|
||||||
|
|
||||||
|
public class IllegalAccountStateException extends RuntimeException {
|
||||||
|
}
|
57
src/main/java/smartBank/TransferSpam.java
Normal file
57
src/main/java/smartBank/TransferSpam.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package smartBank;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class TransferSpam extends Thread{
|
||||||
|
Bank bank;
|
||||||
|
Account sender;
|
||||||
|
Account receiver;
|
||||||
|
|
||||||
|
public TransferSpam(Bank bank, Account sender, Account receiver){
|
||||||
|
this.bank = bank;
|
||||||
|
this.sender = sender;
|
||||||
|
this.receiver = receiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (int i = 0; i < 25; i++) {
|
||||||
|
try {
|
||||||
|
Thread.sleep((int) (Math.random()*500));
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (bank){ // To make Console prettier? very bad to sync the actual transactions
|
||||||
|
synchronized (Account.getLowerIdAccount(this.sender, this.receiver)) {
|
||||||
|
synchronized (Account.getHigherIdAccount(this.sender, this.receiver)) {
|
||||||
|
bank.transfer(this.sender, this.receiver, 1);
|
||||||
|
System.out.println(bank);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main() {
|
||||||
|
Bank bank = new Bank();
|
||||||
|
bank.createAccount();
|
||||||
|
bank.createAccount();
|
||||||
|
bank.createAccount();
|
||||||
|
bank.createAccount();
|
||||||
|
bank.createAccount();
|
||||||
|
bank.setAllBalances(1000);
|
||||||
|
|
||||||
|
TransferSpam t1 = new TransferSpam(bank, bank.allAccounts.get(0), bank.allAccounts.get(1));
|
||||||
|
TransferSpam t2 = new TransferSpam(bank, bank.allAccounts.get(1), bank.allAccounts.get(0));
|
||||||
|
TransferSpam t3 = new TransferSpam(bank, bank.allAccounts.get(2), bank.allAccounts.get(4));
|
||||||
|
TransferSpam t4 = new TransferSpam(bank, bank.allAccounts.get(1), bank.allAccounts.get(3));
|
||||||
|
TransferSpam t5 = new TransferSpam(bank, bank.allAccounts.get(3), bank.allAccounts.get(2));
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
t4.start();
|
||||||
|
t5.start();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user