





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Concurrent Programming, Parallel Programs, JAVA Examples ,Abstract Share Memory, Machine Model
Typology: Slides
1 / 45
This page cannot be seen from the preview
Don't miss anything!






































Prof. Saman Amarasinghe, MIT.
1
6.189 IAP 2007 MIT
2
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
●
●
4
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
●
●
●
Time
Time
5
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
import java.util.*;public class Account {
String id;String password;int balance;Account(String id, String password, String balance) {
this.id = id;this.password = password;this.balance = balance; } boolean is_password(String password) {
return password == this.password; } int getbal() {
return balance; } void post(int v) {
balance = balance + v; } }
import java.util.*;public class Bank {
HashMap<String, Account> accounts;static Bank theBank = null;private Bank() {
accounts = new HashMap<String, Account>(); } public static Bank getbank() {
if (theBank == null)
theBank = new Bank(); return theBank; } public Account get(String ID) {
return accounts.get(ID); } … }
7
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
ATM Account ID >
allyssa Password >
MITROCKS Your account balance is 1000Deposit or Withdraw amount >
-^200 Your account balance is 800
8
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
import java.util.;import java.io.;public class ATM {
static Bank bnk;PrintStream out;BufferedReader in;ATM(PrintStream out, BufferedReader in) {
this.out = out;this.in = in; } public static void main(String[] args) {
bnk = Bank.getbank();BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in)); ATM atm = new ATM(System.out, stdin);atm.run(); }
public void run() {
while(true) {
try {
out.print(
"Account ID >
“^
);
String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print(
"Password >
“^
);
String pass = in.readLine();if (!acc.is_password(pass))
throw new Exception(); out.print(
“^ your balance is
“^
out.print(
"Deposit or withdraw amount >
“
);
int val = in.read();if (acc.getbal() + val > 0)
acc.post(val); else
throw new Exception(); out.print(
“^ your balance is
“^
} catch(Exception e) {
out.println(
"Invalid input, restart
“^ );
} } }
}
10
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
●
●
●
11
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
import java.util.;import java.io.;public class ATM {
static Bank bnk;PrintStream out;BufferedReader in;ATM(PrintStream out, BufferedReader in) {
this.out = out;this.in = in; } public static void main(String[] args) {
bnk = Bank.getbank();BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in)); ATM atm = new ATM(System.out, stdin);atm.run(); }
public void run() {
while(true) {
try {
out.print(
"Account ID >
“ );
String id = in.readLine();String acc = bnk.get(id);if (acc == null) throw new Exception();out.print(
"Password >
“^
);
String pass = in.readLine();if (!acc.is_password(pass))
throw new Exception(); out.print(
“^ your balance is
“^
out.print(
"Deposit or withdraw amount >
“^
);
int val = in.read();if (acc.getbal() + val > 0)
acc.post(val); else
throw new Exception(); out.print(
“^ your balance is
“^
} catch(Exception e) {
out.println(
"Invalid input, restart
“^ );
} } }
}
13
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
ATM 1 Account ID >
allyssa Password >
MITROCKS Your account balance is 1000Deposit or Withdraw amount >
-^200 Your account balance is 800
ben Password >
6189cell Your account balance is 100Deposit or Withdraw amount >
20 Your account balance is 120
14
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
ben Password >
6189cell Your account balance is 100Deposit or Withdraw amount >
- 90
Your account balance is 10
ATM 2 Account ID >
ben Password >
6189cell Your account balance is 100Deposit or Withdraw amount >
- Your account balance is 10
100 - 90 - 90 = 10!!!
16
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
ATM 1 void post(int v) {
ATM 2 void post(int v) {
100 100 100 10 10
17
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
All the interleavings of the threads are NOTacceptable correct programs. ●
Java provides synchronization mechanism to restrictthe interleavings ●
Synchronization serves two purposes:
Ensure safety
for shared updates
-^
Coordinate
actions of threads
-^
-^
19
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
●
●
Courtesy of Emery Berger @ UMASS
20
6.189 IAP 2007 MIT
Prof. Saman Amarasinghe, MIT.
(no
milk
&&
no
note)
leave
note
buy
milk
remove
note
thread B if
(no
milk
&&
no
note)
leave
note
buy
milk
remove
note
●
Does this work?