Commit f23ffb72 by Patryk Czarnik

Przykłady dot. wątków pisane na zajęciach

parent cfc09c1b
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings/
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>PC21-Watki</artifactId>
<version>1.0-SNAPSHOT</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 watki.na_zajeciach;
public class Konto {
private final int numer;
private final String wlasciciel;
private int saldo;
public Konto(int numer, String wlasciciel, int saldo) {
this.numer = numer;
this.wlasciciel = wlasciciel;
this.saldo = saldo;
}
public int getNumer() {
return numer;
}
public String getWlasciciel() {
return wlasciciel;
}
public int getSaldo() {
return saldo;
}
@Override
public String toString() {
return "Konto nr " + numer + ", wł: " + wlasciciel + ", saldo: " + saldo;
}
public synchronized void wplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota nie jest dodatnia!");
}
saldo += kwota;
notify();
}
public synchronized void wyplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota nie jest dodatnia!");
}
if(kwota > saldo) {
throw new RuntimeException("Brak środków!");
}
saldo -= kwota;
}
public synchronized void wyplataCzekaj(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("Kwota nie jest dodatnia!");
}
try {
while(kwota > saldo) {
wait();
}
saldo -= kwota;
notify();
} catch (InterruptedException e) {
}
}
}
package watki.na_zajeciach;
public class Podstawy1 {
public static void main(String[] args) {
System.out.println("Początek main. Tworzę wątki");
Thread watekA = new Thread(() -> {
System.out.println("A: początek");
for(int i = 1; i <= 1000; i++) {
System.out.println("A: " + i);
}
System.out.println("A: koniec");
});
Thread watekB = new Thread(() -> {
System.out.println("B: początek");
for(int i = 1; i <= 1000; i++) {
System.out.println("B: " + i);
}
System.out.println("B: koniec");
});
System.out.println("Uruchamiam wątki");
watekA.start();
watekB.start();
System.out.println("Koniec main");
}
}
package watki.na_zajeciach;
public class Przeploty {
static final int LICZBA_POWTORZEN = 100_000;
static final int KWOTA = 10;
public static void main(String[] args) {
Konto konto = new Konto(1, "Ala", 1000000);
System.out.println(konto);
Thread wplacacz = new Thread(() -> {
for (int i = 0; i < LICZBA_POWTORZEN; i++) {
konto.wplata(KWOTA);
}
});
Thread wyplacacz = new Thread(() -> {
for (int i = 0; i < LICZBA_POWTORZEN; i++) {
konto.wyplata(KWOTA);
}
});
System.out.println("Start");
wplacacz.start();
wyplacacz.start();
// main czeka, aż te wątki się zakończą
try {
wplacacz.join();
wyplacacz.join();
} catch (InterruptedException e) {
System.out.println("interrupted");
}
System.out.println("Koniec");
System.out.println(konto);
}
}
package watki.na_zajeciach;
public class WyplacanieBezCzekania {
public static void main(String[] args) {
Konto konto = new Konto(1, "Ala", 8000);
System.out.println(konto);
Thread wplacacz = new Thread(() -> {
for (;;) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
konto.wplata(5000);
System.out.println("wpłacono 5000, saldo = " + konto.getSaldo());
}
});
Thread wyplacacz = new Thread(() -> {
for (;;) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
try {
konto.wyplata(900);
System.out.println("wydano 900, saldo = " + konto.getSaldo());
} catch (Exception e) {
System.out.println(e);
}
}
});
System.out.println("Start");
wplacacz.start();
wyplacacz.start();
}
}
package watki.na_zajeciach;
public class WyplacanieZCzekaniem {
public static void main(String[] args) {
Konto konto = new Konto(1, "Ala", 8000);
System.out.println(konto);
Thread wplacacz = new Thread(() -> {
for (;;) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
konto.wplata(5000);
System.out.println("wpłacono 5000, saldo = " + konto.getSaldo());
}
});
Thread wyplacacz = new Thread(() -> {
for (;;) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
try {
konto.wyplataCzekaj(900);
System.out.println("wydano 900, saldo = " + konto.getSaldo());
} catch (Exception e) {
System.out.println(e);
}
}
});
System.out.println("Start");
wplacacz.start();
wyplacacz.start();
}
}
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