Commit d1ec1e10 by Patryk Czarnik

Projekt na wątki

parent 929ffd09
/target/
/.settings/
/.classpath
/.project
/*.iml
/.idea/
w1
- instalacja i powtórki ....
- wątki
- bazy danych i JDBC
w2
- czym jest Jakarta (Java EE)
- serwlety
- JSP
w3
- JPA / Hibernate (ORM)
- usługi sieciowe SOAP (JAX-WS)
- usługi sieciowe REST (JAX-RS)
w4 i w5
- uzupełnienia w razie opóźnień
- Spring: Spring Boot, Spring MVC (web), REST w Springu,
Spring Data (dostęp do baz danych), podstawy Security
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.alx.kjava</groupId>
<artifactId>PC20-ObiektyIWatki</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package klasy;
import java.util.Objects;
public class Konto {
private final int numer;
private int saldo;
private String wlasciciel;
public Konto(int numer, int saldo, String wlasciciel) {
this.numer = numer;
this.saldo = saldo;
this.wlasciciel = wlasciciel;
}
public String getWlasciciel() {
return wlasciciel;
}
public void setWlasciciel(String wlasciciel) {
this.wlasciciel = wlasciciel;
}
public int getNumer() {
return numer;
}
public int getSaldo() {
return saldo;
}
public void wplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota wpłaty nie jest dodatnia");
}
this.saldo += kwota;
}
public void wyplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota wypłaty nie jest dodatnia");
}
if(kwota > saldo) {
throw new IllegalArgumentException("Kwota wypłaty przekracza saldo");
}
this.saldo -= kwota;
}
@Override
public String toString() {
return "Konto nr " + numer + ", saldo=" + saldo + ", wlasciciel=" + wlasciciel;
}
@Override
public int hashCode() {
return Objects.hash(numer, saldo, wlasciciel);
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Konto other = (Konto) obj;
return numer == other.numer && saldo == other.saldo && Objects.equals(wlasciciel, other.wlasciciel);
}
}
package klasy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Mutowalnosc1 {
static void proba1(String s, StringBuilder b) {
System.out.println(s);
System.out.println(b);
s += " ma kota"; // tutaj jest tworzony nowy obiekt String
s.concat(" i psa");
b.append(" ma rysia"); // zmiana wewnątrz obiektu
System.out.println(s);
System.out.println(b);
}
static void proba2(int x, Integer y, AtomicInteger z) {
System.out.println(x + " | " + y + " | " + z);
x += 1;
y += 1; // tutaj jest tworzony nowy obiekt pod innym adresem
z.incrementAndGet(); // ta zmiana jest wewnątrz obiektu
System.out.println(x + " | " + y + " | " + z);
}
static void zmienListe(List<String> lista) {
lista.add("nowy element");
}
public static void main(String[] args) {
String s = "Ala";
StringBuilder b = new StringBuilder("Sierotka");
proba1(s, b);
System.out.println(s);
System.out.println(b);
s.replace("A", "O"); // metoda zwraca nowy obiekt jako wynik, ale nie zmienia s
System.out.println(s);
s = s.replace("A", "E");
System.out.println(s);
System.out.println();
int i = 100;
Integer j = 200;
AtomicInteger atom = new AtomicInteger(300);
proba2(i, j, atom);
System.out.println(i + " | " + j + " | " + atom);
System.out.println();
List<String> lista1 = new ArrayList<>();
lista1.add("Ala");
lista1.add("Ola");
System.out.println(lista1);
zmienListe(lista1);
System.out.println(lista1);
List<String> lista2 = List.of("Jan", "Tadeusz");
System.out.println(lista2);
// zmienListe(lista2);
// System.out.println(lista2);
}
}
package klasy;
public class Referencje1 {
public static void main(String[] args) {
Konto a = new Konto(1, 1000, "Ala");
Konto b = new Konto(2, 2000, "Ola");
System.out.println("a: " + a);
System.out.println("b: " + b);
Konto c = b;
System.out.println("c: " + c);
System.out.println();
b.wplata(48);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println();
b = a;
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println();
}
}
package pierwsze_watki;
public class DwaWatki {
public static void main(String[] args) {
System.out.println("Początek main");
Thread watekA = new Thread(() -> {
System.out.println("AAA początek");
for(int i = 1; i <= 100; i++) {
System.out.println("AAA " + i);
}
System.out.println("AAA koniec");
});
Thread watekB = new Thread(() -> {
System.out.println("BBB początek");
for(int i = 1; i <= 100; i++) {
System.out.println("BBB " + i);
}
System.out.println("BBB koniec");
});
System.out.println(watekA);
System.out.println(watekB);
// watekA.setDaemon(true);
// watekB.setDaemon(true);
watekA.start();
watekB.start();
// try {
// System.out.println("main czeka na zakończenie wątków");
// watekA.join();
// watekB.join();
// } catch(InterruptedException e) {
// }
System.out.println("Koniec main");
}
}
package pierwsze_watki;
import java.util.Objects;
public class Konto {
private final int numer;
private int saldo;
private String wlasciciel;
public Konto(int numer, int saldo, String wlasciciel) {
this.numer = numer;
this.saldo = saldo;
this.wlasciciel = wlasciciel;
}
public String getWlasciciel() {
return wlasciciel;
}
public void setWlasciciel(String wlasciciel) {
this.wlasciciel = wlasciciel;
}
public int getNumer() {
return numer;
}
public synchronized int getSaldo() {
return saldo;
}
public synchronized void wplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota wpłaty nie jest dodatnia");
}
this.saldo += kwota;
this.notify();
}
public synchronized void wyplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota wypłaty nie jest dodatnia");
}
if(kwota > this.saldo) {
throw new IllegalArgumentException("Kwota wypłaty przekracza saldo");
}
this.saldo -= kwota;
}
public synchronized void wyplataCzekaj(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota wypłaty nie jest dodatnia");
}
try {
while(kwota > this.saldo) {
this.wait();
}
this.saldo -= kwota;
this.notify();
} catch(InterruptedException e) {
}
}
@Override
public String toString() {
return "Konto nr " + numer + ", saldo=" + saldo + ", wlasciciel=" + wlasciciel;
}
@Override
public int hashCode() {
return Objects.hash(numer, saldo, wlasciciel);
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Konto other = (Konto) obj;
return numer == other.numer && saldo == other.saldo && Objects.equals(wlasciciel, other.wlasciciel);
}
}
package pierwsze_watki;
public class PrzykladKonto1 {
public static void main(String[] args) {
Konto konto = new Konto(1, 1_000_000, "Ala");
Thread wplacacz = new Thread(() -> {
for(int i = 0; i < 100_000; i++) {
konto.wplata(10);
}
});
Thread wyplacacz = new Thread(() -> {
for(int i = 0; i < 100_000; i++) {
konto.wyplata(10);
}
});
System.out.println(konto);
System.out.println("uruchamiam wątki...");
try {
wplacacz.start();
wyplacacz.start();
wplacacz.join();
wyplacacz.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("wątki zakończone");
System.out.println(konto);
}
}
package pierwsze_watki;
import java.time.Duration;
public class PrzykladKonto2a {
public static void main(String[] args) {
Konto konto = new Konto(1, 25_000, "Ala");
Thread wplacacz = new Thread(() -> {
for(;;) {
try {
Thread.sleep(Duration.ofSeconds(3));
} catch(InterruptedException e) {
break;
}
konto.wplata(10_000);
System.out.println("po wpłata: " + konto.getSaldo());
}
});
Thread wyplacacz = new Thread(() -> {
for(;;) {
try {
Thread.sleep(Duration.ofMillis(150));
konto.wyplata(1000);
System.out.println("po wypłata: " + konto.getSaldo());
} catch(InterruptedException e) {
break;
} catch(Exception e) {
System.err.println(e.getMessage());
}
}
});
System.out.println(konto);
System.out.println("uruchamiam wątki...");
wplacacz.start();
wyplacacz.start();
// program należy kończyć "zabijając" w IDE lub Ctrl+C w konsoli
}
}
package pierwsze_watki;
import java.time.Duration;
public class PrzykladKonto2b {
public static void main(String[] args) {
Konto konto = new Konto(1, 25_000, "Ala");
Thread wplacacz = new Thread(() -> {
for(;;) {
try {
Thread.sleep(Duration.ofSeconds(3));
} catch(InterruptedException e) {
break;
}
konto.wplata(10_000);
System.out.println("po wpłata: " + konto.getSaldo());
}
});
Thread wyplacacz = new Thread(() -> {
for(;;) {
try {
Thread.sleep(Duration.ofMillis(150));
konto.wyplataCzekaj(970);
System.out.println("po wypłata: " + konto.getSaldo());
} catch(InterruptedException e) {
break;
} catch(Exception e) {
System.err.println(e.getMessage());
}
}
});
System.out.println(konto);
System.out.println("uruchamiam wątki...");
wplacacz.start();
wyplacacz.start();
// program należy kończyć "zabijając" w IDE lub Ctrl+C w konsoli
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment