Commit 13bb6d93 by Patryk Czarnik

watki zmiany

parent f2df6e89
......@@ -8,7 +8,7 @@ public class Afrykarium3 {
public void wpuśćWycieczkę(int ilu) {
try {
// zmniejsza wartość semafora o ilu,
// zmniejsza wartość semafora o ilu,
// ale jeśli wartość semafora < ilu, to czeka, aż semafor uzyska odp. wartość.
miejsca.acquire(ilu); // -= akademicko : P
} catch(InterruptedException e) {
......
......@@ -29,10 +29,10 @@ public class ConcMap {
private static final int R = 10_000;
// odkomentuj jedną z wersji i sprawdź
//private final Map<String, Integer> map = Collections.synchronizedMap(new HashMap<>());
// private final Map<String, Integer> map = new HashMap<>(); // uwaga - to będzie dawać błędy "nadpisanych danych"
// private final Map<String, Integer> map = Collections.synchronizedMap(new HashMap<>());
private final Map<String, Integer> map = new ConcurrentHashMap<>();
//private final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>();
// private final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>();
private void dzialaj() {
final ThreadLocalRandom random = ThreadLocalRandom.current();
......
......@@ -48,7 +48,6 @@ public class ProdKons1 {
} catch (InterruptedException e) {
}
}
}
}
......
......@@ -47,7 +47,7 @@ class Konto {
throw new IllegalArgumentException("Ujemna kwota " + kwota + " w wypłacie");
}
if(kwota > saldo) {
throw new BrakSrodkow("Brak środków na koncie nr " + numer);
throw new BrakSrodkow("Brak środków na koncie nr " + numer);
}
saldo -= kwota;
......
......@@ -77,7 +77,6 @@ class Konto {
if(kwota > saldo) {
throw new BrakSrodkow("Brak środków na koncie nr " + numer);
}
saldo -= kwota;
} finally {
lock.unlock();
......
......@@ -29,7 +29,6 @@ public class Przerwanie {
System.in.read();
// czekamy na ENTER i gdy zostanie naciśnięty, wywołamy interrupt na wątku
th.interrupt();
} catch (IOException e) {
e.printStackTrace();
};
......
......@@ -23,7 +23,7 @@ public class Watki2_Join {
System.out.println("stan przed join: " + th1.getState());
// wątek main czeka na zakończenie wątków th1, th2, th3
// jeśli one się skończyły wcześniej, to od razu przechodzi dalej
// jeśli one się skończyły wcześniej, to od razu przechodzi dalej
try {
th1.join();
th2.join();
......
package gotowe.p50_watki.pule;
import java.util.concurrent.*;
public class CustomThreadPool {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2, 4, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(10), // Ograniczona kolejka
new ThreadPoolExecutor.CallerRunsPolicy() // Zadania są wykonywane przez wątek wywołujący w razie braku miejsca w tej puli
);
executor.submit(() -> System.out.println("Task executed"));
executor.shutdown();
}
}
package gotowe.p50_watki.pule;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
public class PuleWatkow {
......@@ -17,7 +13,7 @@ public class PuleWatkow {
// ExecutorService pool = Executors.newWorkStealingPool(); // od Javy 8 - stara się wykorzystać wszystkie procesory
// ExecutorService pool = Executors.newWorkStealingPool(2); // wersja z ograniczeniem współbieżności do pewnego poziomu
// ScheduledExecutorService pool = Executors.newScheduledThreadPool(20); // pozwala planować zadania na przyszłość
Procedura zadanie = new Procedura();
System.out.println("Zaczynam zlecać...");
for(int i = 0; i < N; i++) {
......@@ -33,7 +29,7 @@ public class PuleWatkow {
System.out.println("Zlecilem wykonanie");
pool.shutdown();
//pool.shutdownNow();
// pool.shutdownNow();
System.out.println("Po shutdown");
//pool.submit(() -> {});
try {
......
......@@ -29,6 +29,7 @@ public class PuleWatkowCallable {
pool.shutdown();
System.out.println("Po shutdown");
System.out.println(gfuture.isDone());
// gfuture.cancel(false);
String wynik = gfuture.get(); // to powoduje oczekiwanie na zakończenie zadania
System.out.println(gfuture.isDone() + wynik);
//pool.shutdownNow();
......
package gotowe.p50_watki.pule;
import java.util.concurrent.*;
public class PuleWatkow_Lambda {
public static void main(String[] args) throws InterruptedException, ExecutionException {
try(ExecutorService pool = Executors.newFixedThreadPool(4)) {
Future<?> futureVoid = pool.submit(() -> {
System.out.println("Zadanie Runnable");
});
Future<String> futureString = pool.submit(() -> {
System.out.println("Zadanie Callable");
return "to jest wynik";
});
while(! futureString.isDone()) {
System.out.println("czekam na koniec String");
}
String s = futureString.get();
System.out.println("gotowe: " + s);
}
}
}
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