26. ตัว อย่า งโปรแกรม
public class BankSystemUnSync{
public class BankSystemUnSync{
public static void main(String[] args){
public static void main(String[] args){
final int NACCOUNTS = 10;
final int NACCOUNTS = 10;
final int INITIAL_BALANCE = 10000;
final int INITIAL_BALANCE = 10000;
Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
int i;
int i;
for (i = 0; i < NACCOUNTS; i++) {
for (i = 0; i < NACCOUNTS; i++) {
Transfer t = new Transfer(b, i,INITIAL_BALANCE);
Transfer t = new Transfer(b, i,INITIAL_BALANCE);
t.setPriority(Thread.NORM_PRIORITY + i % 2);
t.setPriority(Thread.NORM_PRIORITY + i % 2);
t.start();
t.start();
}}
}}
}}
27. ตัว อย่า งโปรแกรม
class Bank{
class Bank{
public static final int NTEST = 10000;
public static final int NTEST = 10000;
private final int[] accounts;
private final int[] accounts;
private long ntransacts = 0;
private long ntransacts = 0;
public Bank(int n, int initialBalance) {
public Bank(int n, int initialBalance) {
accounts = new int[n];
accounts = new int[n];
for (int i = 0; i < accounts.length; i++) {
for (int i = 0; i < accounts.length; i++) {
accounts[i] = initialBalance;
accounts[i] = initialBalance;
}}
ntransacts = 0;
ntransacts = 0;
}}
public void transfer(int from, int to, int amount) {
public void transfer(int from, int to, int amount) {
if (accounts[from] < amount) {
if (accounts[from] < amount) {
return;
return;
}}
28. ตัว อย่า งโปรแกรม
accounts[from] -= amount;
accounts[from] -= amount;
accounts[to] += amount;
accounts[to] += amount;
ntransacts++;
ntransacts++;
if (ntransacts % NTEST == 0) {
if (ntransacts % NTEST == 0) {
test();
test();
}}
}}
public void test(){
public void test(){
int sum = 0;
int sum = 0;
for (int i = 0; i < accounts.length; i++){
for (int i = 0; i < accounts.length; i++){
sum += accounts[i];
sum += accounts[i];
}}
System.out.println("Transactions:" + ntransacts
System.out.println("Transactions:" + ntransacts
+ " Sum: " + sum);
+ " Sum: " + sum);
}}
public int size(){
public int size(){
return accounts.length;
return accounts.length;
}}
}}
29. ตัว อย่า งโปรแกรม
class Transfer extends Thread{
class Transfer extends Thread{
private Bank bank;
private Bank bank;
private int fromAccount;
private int fromAccount;
private int maxAmount;
private int maxAmount;
public Transfer(Bank b, int from, int max) {
public Transfer(Bank b, int from, int max) {
bank = b;
bank = b;
fromAccount = from;
fromAccount = from;
maxAmount = max;
maxAmount = max;
}}
public void run() {
public void run() {
try {
try {
while (!interrupted()) {
while (!interrupted()) {
int toAccount = (int)(bank.size() * Math.random());
int toAccount = (int)(bank.size() * Math.random());
int amount = (int)(maxAmount * Math.random());
int amount = (int)(maxAmount * Math.random());
bank.transfer(fromAccount, toAccount, amount);
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep((int) (Math.random() * 10));
Thread.sleep((int) (Math.random() * 10));
}}
}}
catch(InterruptedException e) {}
catch(InterruptedException e) {}
}}
}}
30. ตัว อย่า งโปรแกรม
class TransferThread extends Thread {{
class TransferThread extends Thread
private Bank bank;
private Bank bank;
private int sourceAcct;
private int sourceAcct;
private int destAcct;
private int destAcct;
public TransferThread(Bank b,int s,int d) {{
public TransferThread(Bank b,int s,int d)
bank == b;
bank b;
sourceAcct == s;
sourceAcct s;
destAcct == d;
destAcct d;
}}
public void run() {{
public void run()
try {{
try
while (!interrupted()) {{
while (!interrupted())
bank.transfer(sourceAcct,destAcct);
bank.transfer(sourceAcct,destAcct);
sleep(1);
sleep(1);
}}
}} catch (Exception e) {{ }}
catch (Exception e)
}}
}}
37. ตัว อย่า งการใช้ค ีย ์เ วิร ์ด synchronized
public class BankSystem {
public class BankSystem {
public static void main(String[] args) {
public static void main(String[] args) {
final int NACCOUNTS = 10;
final int NACCOUNTS = 10;
final int INITIAL_BALANCE = 10000;
final int INITIAL_BALANCE = 10000;
Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
Bank b = new Bank(NACCOUNTS, INITIAL_BALANCE);
int i;
int i;
for (i = 0; i < NACCOUNTS; i++) {
for (i = 0; i < NACCOUNTS; i++) {
Transfer t = new Transfer(b, i, INITIAL_BALANCE);
Transfer t = new Transfer(b, i, INITIAL_BALANCE);
t.setPriority(Thread.NORM_PRIORITY + i % 2);
t.setPriority(Thread.NORM_PRIORITY + i % 2);
t.start();
t.start();
}}
}}
}}
38. ตัว อย่า งการใช้ค ีย ์เ วิร ์ด synchronized
class Bank {
class Bank {
public static final int NTEST = 10000;
public static final int NTEST = 10000;
private final int[] accounts;
private final int[] accounts;
private long ntransacts = 0;
private long ntransacts = 0;
public Bank(int n, int initialBalance) {
public Bank(int n, int initialBalance) {
accounts = new int[n];
accounts = new int[n];
for (int i = 0; i < accounts.length; i++) {
for (int i = 0; i < accounts.length; i++) {
accounts[i] = initialBalance;
accounts[i] = initialBalance;
}}
ntransacts = 0;
ntransacts = 0;
}}
public synchronized void transfer(int from, int to, int
public synchronized void transfer(int from, int to, int
amount) {
amount) {
try{
try{
while (accounts[from] < amount){
while (accounts[from] < amount){
wait();
wait();
}}
39. ตัว อย่า งการใช้ค ีย ์เ วิร ์ด synchronized
accounts[from] -= amount;
accounts[from] -= amount;
accounts[to] += amount;
accounts[to] += amount;
ntransacts++;
ntransacts++;
notifyAll();
notifyAll();
if (ntransacts % NTEST == 0) {
if (ntransacts % NTEST == 0) {
test();
test();
}}
} catch(InterruptedException e) {}
} catch(InterruptedException e) {}
}}
public synchronized void test() {
public synchronized void test() {
int sum = 0;
int sum = 0;
for (int i = 0; i < accounts.length; i++){
for (int i = 0; i < accounts.length; i++){
sum += accounts[i];
sum += accounts[i];
}}
System.out.println("Transactions:" + ntransacts
System.out.println("Transactions:" + ntransacts
+ " Sum: " + sum);
+ " Sum: " + sum);
}}
40. ตัว อย่า งการใช้ค ีย ์เ วิร ์ด synchronized
public int size(){
public int size(){
return accounts.length;
return accounts.length;
}}
}}
class Transfer extends Thread{
class Transfer extends Thread{
private Bank bank;
private Bank bank;
private int fromAccount;
private int fromAccount;
private int maxAmount;
private int maxAmount;
public Transfer(Bank b, int from, int max){
public Transfer(Bank b, int from, int max){
bank = b;
bank = b;
fromAccount = from;
fromAccount = from;
maxAmount = max;
maxAmount = max;
}}
41. ตัว อย่า งการใช้ค ีย ์เ วิร ์ด synchronized
public void run() {
public void run() {
try {
try {
while (!interrupted()) {
while (!interrupted()) {
int toAccount = (int)(bank.size() * Math.random());
int toAccount = (int)(bank.size() * Math.random());
int amount = (int)(maxAmount * Math.random());
int amount = (int)(maxAmount * Math.random());
bank.transfer(fromAccount, toAccount, amount);
bank.transfer(fromAccount, toAccount, amount);
Thread.sleep(1);
Thread.sleep(1);
}}
}}
catch(InterruptedException e) {}
catch(InterruptedException e) {}
}}
}}