Skip to content

Commit f79d79e

Browse files
committed
SynchroStack rewritten with some debug logs
1 parent 4bec067 commit f79d79e

File tree

11 files changed

+564
-122
lines changed

11 files changed

+564
-122
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ My formatted Lab tasks: [TASKS.md](TASKS.md)
1212
1. Clone this repository
1313
```bash
1414
git clone https://github.com/cypotat/Java-Labs.git
15-
cd Java-Labs
15+
cd Java-Labs/src
1616
```
1717
2. Compile the specific lab
1818

1919
```bash
20-
cd src
2120
javac labs/sem1/lab1/*.java
2221
```
2322

src/labs/sem1/lab10/Main.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,43 @@
33
public class Main {
44
public static void main(String[] args) {
55
SynchroStack stack = new SynchroStack();
6-
for (int i = 0; i < 10; i++) {
6+
Utils.initCSV();
7+
8+
for (int i = 0; i < 100; i++) {
79
stack.push(i);
810
}
911

10-
System.out.println(stack);
11-
12-
// multiple threads
12+
Thread[] threads = new Thread[4];
1313

14-
Thread t1 = new Thread(new Runnable() {
15-
@Override
16-
public void run() {
17-
for (int i = 0; i < 10; i++) {
18-
stack.push(i);
14+
for (int i = 0; i < 2; i++) {
15+
threads[i * 2] = new Thread(() -> {
16+
Utils.printRow("START - Stack 10 pushes");
17+
for (int j = 0; j < 10; j++) {
18+
stack.push(j);
1919
}
20-
}
21-
});
20+
Utils.printRow("END - Stack 10 pushes");
21+
});
2222

23-
Thread t2 = new Thread(new Runnable() {
24-
@Override
25-
public void run() {
26-
for (int i = 0; i < 10; i++) {
23+
threads[i * 2 + 1] = new Thread(() -> {
24+
Utils.printRow("START - Stack 10 pops");
25+
for (int j = 0; j < 10; j++) {
2726
stack.pop();
2827
}
29-
}
30-
});
31-
32-
t1.start();
33-
t2.start();
28+
Utils.printRow("END - Stack 10 pops");
29+
});
30+
}
3431

35-
try {
36-
t1.join();
37-
t2.join();
38-
} catch (InterruptedException e) {
39-
e.printStackTrace();
32+
for (int i = 0; i < threads.length; i++) {
33+
threads[i].start();
4034
}
4135

42-
System.out.println(stack);
36+
for (int i = 0; i < threads.length; i++) {
37+
try {
38+
threads[i].join();
39+
Utils.printRow("END - Thread " + i + " join");
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
}
4344
}
4445
}

src/labs/sem1/lab10/Stack.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package labs.sem1.lab10;
2+
3+
// class Stack - parent class for SynchroStack and SynchroStackFast
4+
5+
public class Stack {
6+
protected StackNode head;
7+
protected int size;
8+
9+
public void push(int i) {
10+
StackNode node = new StackNode(i);
11+
node.next = head;
12+
head = node;
13+
size++;
14+
}
15+
16+
public int pop() throws RuntimeException {
17+
if (head == null) {
18+
throw new RuntimeException("Stack is empty");
19+
}
20+
int value = head.value;
21+
head = head.next;
22+
size--;
23+
return value;
24+
}
25+
26+
public int size() {
27+
return size;
28+
}
29+
30+
public boolean equals(Object o) {
31+
if (o == null) {
32+
return false;
33+
}
34+
if (o == this) {
35+
return true;
36+
}
37+
if (!(o instanceof Stack)) {
38+
return false;
39+
}
40+
Stack other = (Stack) o;
41+
StackNode node1 = head;
42+
StackNode node2 = other.head;
43+
while (node1 != null && node2 != null) {
44+
if (node1.value != node2.value) {
45+
return false;
46+
}
47+
node1 = node1.next;
48+
node2 = node2.next;
49+
}
50+
return node1 == null && node2 == null;
51+
}
52+
53+
public String toString() {
54+
StringBuilder sb = new StringBuilder();
55+
sb.append("[");
56+
StackNode node = head;
57+
while (node != null) {
58+
sb.append(node.value);
59+
sb.append(", ");
60+
node = node.next;
61+
}
62+
if (head != null) {
63+
sb.delete(sb.length() - 2, sb.length());
64+
}
65+
sb.append("]");
66+
return sb.toString();
67+
}
68+
69+
class StackNode {
70+
int value;
71+
StackNode next;
72+
73+
StackNode(int value) {
74+
this.value = value;
75+
}
76+
}
77+
}
Lines changed: 18 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,31 @@
11
package labs.sem1.lab10;
22

3-
// SynchroStack:
4-
// Реализовать класс Stack, используя связный список.
5-
// Добавить synchronized ключевые слова к методам push() и pop() для защиты от многопоточной модификации.
6-
7-
class Stack {
8-
private StackNode top;
9-
10-
public void push(int i) {
11-
StackNode node = new StackNode(i);
12-
node.next = top;
13-
top = node;
14-
}
15-
16-
public int pop() {
17-
if (top == null) {
18-
throw new RuntimeException("Stack is empty");
19-
// return 0;
20-
}
21-
int value = top.value;
22-
top = top.next;
23-
return value;
24-
}
25-
26-
public boolean equals(Object o) {
27-
if (o == null) {
28-
return false;
29-
}
30-
if (o == this) {
31-
return true;
32-
}
33-
if (!(o instanceof Stack)) {
34-
return false;
35-
}
36-
Stack other = (Stack) o;
37-
StackNode node1 = top;
38-
StackNode node2 = other.top;
39-
while (node1 != null && node2 != null) {
40-
if (node1.value != node2.value) {
41-
return false;
42-
}
43-
node1 = node1.next;
44-
node2 = node2.next;
45-
}
46-
return node1 == null && node2 == null;
47-
}
48-
49-
public String toString() {
50-
StringBuilder sb = new StringBuilder();
51-
sb.append("[");
52-
StackNode node = top;
53-
while (node != null) {
54-
sb.append(node.value);
55-
sb.append(", ");
56-
node = node.next;
57-
}
58-
if (top != null) {
59-
sb.delete(sb.length() - 2, sb.length());
60-
}
61-
sb.append("]");
62-
return sb.toString();
63-
}
64-
65-
class StackNode {
66-
int value;
67-
StackNode next;
68-
69-
StackNode(int value) {
70-
this.value = value;
71-
}
72-
}
73-
}
74-
75-
public class SynchroStack {
76-
private Stack stack = new Stack();
3+
public class SynchroStack extends Stack {
774

785
public synchronized void push(int i) {
79-
System.out.println("Thread: " + Thread.currentThread().getName() + " locked stack");
80-
synchronized (stack) {
81-
stack.push(i);
82-
System.out.println("Thread: " + Thread.currentThread().getName() + " unlocked stack");
83-
}
6+
Utils.printRow("START - Push");
7+
super.push(i);
8+
Utils.printRow("END - Push");
849
}
8510

8611
public synchronized int pop() {
87-
System.out.println("Thread: " + Thread.currentThread().getName() + " locked stack");
88-
synchronized (stack) {
89-
int value = stack.pop();
90-
System.out.println("Thread: " + Thread.currentThread().getName() + " unlocked stack");
91-
return value;
92-
}
12+
Utils.printRow("START - Pop");
13+
int i = super.pop();
14+
Utils.printRow("END - Pop");
15+
return i;
9316
}
9417

95-
public boolean equals(Object o) {
96-
synchronized (stack) {
97-
return stack.equals(o);
98-
}
18+
public synchronized boolean equals(Object o) {
19+
Utils.printRow("START - Equals");
20+
boolean b = super.equals(o);
21+
Utils.printRow("END - Equals");
22+
return b;
9923
}
10024

101-
public String toString() {
102-
synchronized (stack) {
103-
return stack.toString();
104-
}
25+
public synchronized String toString() {
26+
Utils.printRow("START - toString");
27+
String s = super.toString();
28+
Utils.printRow("END - toString");
29+
return s;
10530
}
10631
}
22.7 KB
Binary file not shown.

src/labs/sem1/lab10/Utils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package labs.sem1.lab10;
2+
3+
public class Utils {
4+
private static int threadsCount = 4;
5+
6+
public static void initCSV() {
7+
// generate string with headers (e.g. "main,Thread-0,Thread-1,...")
8+
String[] headers = new String[threadsCount + 1];
9+
headers[0] = "main";
10+
for (int i = 1; i < headers.length; i++) {
11+
headers[i] = "Thread-" + (i - 1);
12+
}
13+
System.out.println(String.join(",", headers));
14+
}
15+
16+
public static void printRow(String s) {
17+
// print s at the column of the current thread
18+
// other columns should be empty
19+
String[] row = new String[threadsCount + 1];
20+
for (int i = 0; i < row.length; i++) {
21+
row[i] = "\"\"";
22+
}
23+
// get current thread id from "Thread-0" -> 1 (main -> 0)
24+
String threadName = Thread.currentThread().getName();
25+
if (threadName.equals("main")) {
26+
row[0] = s;
27+
} else {
28+
row[Integer.parseInt(threadName.substring(7)) + 1] = s;
29+
}
30+
System.out.println(String.join(",", row));
31+
}
32+
}

0 commit comments

Comments
 (0)