Esempi di sincronizzazione tra thread
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
40
src/com/oracle/java8/professional/concurrency/Data.java
Normal file
40
src/com/oracle/java8/professional/concurrency/Data.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
29
src/com/oracle/java8/professional/concurrency/PingPong.java
Normal file
29
src/com/oracle/java8/professional/concurrency/PingPong.java
Normal 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();
|
||||
}
|
||||
}
|
||||
31
src/com/oracle/java8/professional/concurrency/Receiver.java
Normal file
31
src/com/oracle/java8/professional/concurrency/Receiver.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
37
src/com/oracle/java8/professional/concurrency/Sender.java
Normal file
37
src/com/oracle/java8/professional/concurrency/Sender.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user