Esempi di sincronizzazione tra thread

This commit is contained in:
Fabio Scotto di Santolo
2020-05-10 11:53:01 +02:00
parent a80d64735b
commit 9b85eed528
9 changed files with 315 additions and 172 deletions

View File

@@ -0,0 +1,19 @@
package com.oracle.java8.professional.concurrency;
public class BankAccount {
private long number;
private long balance;
public BankAccount(long number, long balance) {
this.number = number;
this.balance = balance;
}
public synchronized long getBalance() {
return balance;
}
public synchronized void deposit(long amout) {
balance += amout;
}
}

View File

@@ -0,0 +1,40 @@
package com.oracle.java8.professional.concurrency;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Data {
private String packet;
private boolean transfer = true;
public synchronized void send(String packet) {
while (!transfer) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Logger.getAnonymousLogger().log(Level.SEVERE, "Interrupted error", e);
}
}
transfer = false;
this.packet = packet;
notifyAll();
}
public synchronized String receive() {
while (transfer) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Logger.getAnonymousLogger().log(Level.SEVERE, "Interrupted error", e);
}
}
transfer = true;
notifyAll();
return packet;
}
}

View File

@@ -0,0 +1,29 @@
package com.oracle.java8.professional.concurrency;
public class PingPong extends Thread {
private String word;
private int delay;
public PingPong(String whatToSay, int delayTime) {
this.word = whatToSay;
this.delay = delayTime;
}
@Override
public void run() {
try {
for (;;) {
System.out.println(word + " ");
Thread.sleep(delay);
}
} catch (InterruptedException e) {
return;
}
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
new PingPong("ping", 33).start();
new PingPong("PONG", 100).start();
}
}

View File

@@ -0,0 +1,31 @@
package com.oracle.java8.professional.concurrency;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Receiver implements Runnable {
private Data load;
public Receiver(Data data) {
this.load = data;
}
public void run() {
for (String receivedMessage = load.receive();
!"End".equals(receivedMessage);
receivedMessage = load.receive()) {
System.out.println(receivedMessage);
// ...
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Logger.getAnonymousLogger().log(Level.SEVERE, "Interrupted error", e);
}
}
}
}

View File

@@ -0,0 +1,37 @@
package com.oracle.java8.professional.concurrency;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Sender implements Runnable {
private Data data;
public Sender(Data data) {
this.data = data;
}
@Override
public void run() {
String packets[] = {
"First packet",
"Second packet",
"Third packet",
"Fourth packet",
"End"
};
for (String packet : packets) {
data.send(packet);
// Thread.sleep() to mimic heavy server-side processing
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Logger.getAnonymousLogger().log(Level.SEVERE, "Interrupted error", e);
}
}
}
}