Exercise four

Synchronization of Selling Tickets at the Window

1
2
3
4
5
6
7
8
package MultiThread;

public class Main {
public static void main(String[] args) {
Window w = new Window();
w.window();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package MultiThread;

class Test implements Runnable {
private static int num = 1;
int f = 0;

@Override
public void run() {
while(true) {
synchronized(this) {
if(num <= 20) {
System.out.println(Thread.currentThread().getName() + ": ticket " + num++);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
if(f == 0) {
System.out.println("Sold Out");
f = 1;
}
return;
}
}
}
}

}


public class Window {
void window() {
Test tr = new Test();
Thread t1 = new Thread(tr, "Window 1");
Thread t2 = new Thread(tr, "Window 2");
Thread t3 = new Thread(tr, "Window 3");
t1.start();
t2.start();
t3.start();
}
}

Inter-Thread Synchronization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package MultiThread1;

class Test implements Runnable {
private static int aaa = 100;
private static int bbb = 0;
private static int ccc = 0;

@Override
public void run() {
while(true) {
synchronized(this) {
if((Thread.currentThread().getName().equals("B") && bbb < 1000)||(Thread.currentThread().getName().equals("C") && ccc < 1000)) {
aaa += 100;
System.out.println("account "+Thread.currentThread().getName()+" transfer $100 to account A, balance of account A: $" + aaa);

if(Thread.currentThread().getName().equals("B")) bbb+=100;
else ccc+=100;

try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
if(bbb >= 1000 && ccc >= 1000){
System.out.println("account B transfer $1000 to account A in total");
System.out.println("account C transfer $1000 to account A in total");
System.out.println("balance of account A: $2100");
}
return;
}
}
}
}

}

public class Bank {
void bank(){
System.out.println("balance of account A: $100");
Test tr = new Test();
Thread t1 = new Thread(tr, "B");
Thread t2 = new Thread(tr, "C");
t1.start();
t2.start();
}
}
1
2
3
4
5
6
7
8
package MultiThread1;

public class Main {
public static void main(String[] args) {
Bank b = new Bank();
b.bank();
}
}

Deadlock: Dining Philosopher Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package MultiThread2;

public class Customer extends Thread {
Text text;

Customer(Text text) {
this.text = text;
}

@Override
public void run() {
int i = 1;
while(true) {
text.Get(i++);
if(i > 10) return;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package MultiThread2;

class Text {
int count = 0;

synchronized void Set(int num) {
while(count == 1) {
try {
wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
System.exit(0);
}
}
int f = num % 2;
if(f == 1) {
System.out.println("Set "+ num +"-> Item1 : 12345");
}else if(f == 0) {
System.out.println("Set "+ num +"-> Item2 : abcde");
}
count = 1;
notifyAll();
}

synchronized void Get(int num) {
while(count == 0) {
try {
wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
System.exit(0);
}
}
int f = num % 2;
if(f == 1) {
System.out.println("Get "+ num +"-> Item1 : 12345");
}else if(f == 0) {
System.out.println("Get "+ num +"-> Item2 : abcde");
}
count = 0;
notifyAll();
}
}


public class Master {
void master(){
Text text = new Text();
new Producer(text).start();
new Customer(text).start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package MultiThread2;

public class Producer extends Thread {
Text text;

Producer(Text text) {
this.text = text;
}

@Override
public void run() {
int i = 1;
while(true) {
text.Set(i++);
if(i > 10) return;
}
}
}
1
2
3
4
5
6
7
8
package MultiThread2;

public class Main {
public static void main(String[] args){
Master m = new Master();
m.master();
}
}

Deadlock: Dining Philosopher Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package Deadlock;

class MEN extends Thread{
private String name;
private Fork cur;
public MEN(String name,Fork cur){
super(name);
this.name=name;
this.cur=cur;
}

public void run(){
while(true){
thinking();
cur.tryTodo();
eating();
cur.OverDo();
}
}

public void eating(){
System.out.println(name + " is eating.");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


public void thinking(){
System.out.println(name + " is thinking.");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class Fork{
private boolean[] flag = {false,false,false,false,false,false};

public synchronized void tryTodo(){
int i = Thread.currentThread().getName().charAt(3)-'0';
int a = i, b = i+1;
if(b == 6) b = 1;
while(flag[a]||flag[b]){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag[a]=true; flag[b]=true;
}

public synchronized void OverDo(){
int i = Thread.currentThread().getName().charAt(3)-'0';
int a = i, b = i+1;
if(b == 6) b = 1;
flag[a]=false; flag[b]=false;
System.out.println(Thread.currentThread().getName() + " is over eating.");
notifyAll();
}
}


public class Deadlock {
public static void main(String []args){
Fork cur = new Fork();
// 1 2 3 4 5 -> 1
for(int i=1; i<=5; i++)
new MEN("men" + i, cur).start();
}
}
Donate? comment?