Skip to content

Commit 9155472

Browse files
committed
SynchroStackFast rewritten and logger optimized
1 parent f79d79e commit 9155472

File tree

8 files changed

+262
-47
lines changed

8 files changed

+262
-47
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package labs.sem1.lab10;
2+
3+
public class DebugStack extends Stack {
4+
protected boolean debug = false;
5+
6+
public void setDebug(boolean debug) {
7+
this.debug = debug;
8+
}
9+
10+
public void push(int i) {
11+
if (debug)
12+
Utils.printRow("Push START");
13+
super.push(i);
14+
if (debug)
15+
Utils.printRow("Push END");
16+
}
17+
18+
public int pop() throws RuntimeException {
19+
if (debug)
20+
Utils.printRow("Pop START");
21+
int i = super.pop();
22+
if (debug)
23+
Utils.printRow("Pop END");
24+
return i;
25+
}
26+
27+
public boolean equals(Object o) {
28+
if (debug)
29+
Utils.printRow("Equals START");
30+
boolean b = super.equals(o);
31+
if (debug)
32+
Utils.printRow("Equals END");
33+
return b;
34+
}
35+
36+
public String toString() {
37+
if (debug)
38+
Utils.printRow("toString START");
39+
String s = super.toString();
40+
if (debug)
41+
Utils.printRow("toString END");
42+
return s;
43+
}
44+
}

src/labs/sem1/lab10/Main.java

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,85 @@
22

33
public class Main {
44
public static void main(String[] args) {
5-
SynchroStack stack = new SynchroStack();
6-
Utils.initCSV();
5+
Class<?>[] classes = { // classes to benchmark
6+
Stack.class,
7+
SynchroStack.class,
8+
SynchroStackFast.class
9+
};
710

8-
for (int i = 0; i < 100; i++) {
9-
stack.push(i);
11+
int threadsPerMethod = 2; // threads per method
12+
13+
// create stacks of each class
14+
Stack[] stacks = new Stack[classes.length];
15+
for (int i = 0; i < classes.length; i++) {
16+
try {
17+
stacks[i] = (Stack) classes[i].getConstructor().newInstance();
18+
// fill stacks with 200 elements
19+
for (int j = 0; j < 200; j++) {
20+
stacks[i].push(j);
21+
}
22+
} catch (Exception e) {
23+
e.printStackTrace();
24+
}
1025
}
1126

12-
Thread[] threads = new Thread[4];
27+
// print type of stacks
28+
for (Stack stack : stacks) {
29+
Runnable[] methods = {
30+
() -> { // push (write operation)
31+
for (int i = 0; i < 1000; i++) {
32+
stack.push(i);
33+
}
34+
},
35+
() -> { // pop (write operation)
36+
for (int i = 0; i < 1000; i++) {
37+
stack.pop();
38+
}
39+
},
40+
() -> { // equals (read operation)
41+
for (int i = 0; i < 10000; i++) {
42+
stack.equals(stack);
43+
}
44+
},
45+
() -> { // toString (read operation)
46+
for (int i = 0; i < 100; i++) {
47+
stack.toString();
48+
}
49+
}
50+
};
1351

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);
52+
53+
54+
// create threads
55+
Thread[] threads = new Thread[methods.length * threadsPerMethod];
56+
for (int i = 0; i < methods.length; i++) {
57+
for (int j = 0; j < threadsPerMethod; j++) {
58+
int index = i * threadsPerMethod + j;
59+
threads[index] = new Thread(methods[i]);
60+
threads[index].setName("Thread-" + index);
1961
}
20-
Utils.printRow("END - Stack 10 pushes");
21-
});
62+
}
63+
64+
System.out.print("Benchmarking " + stack.getClass().getSimpleName());
2265

23-
threads[i * 2 + 1] = new Thread(() -> {
24-
Utils.printRow("START - Stack 10 pops");
25-
for (int j = 0; j < 10; j++) {
26-
stack.pop();
27-
}
28-
Utils.printRow("END - Stack 10 pops");
29-
});
30-
}
66+
// start threads and measure time
67+
long start = System.currentTimeMillis();
3168

32-
for (int i = 0; i < threads.length; i++) {
33-
threads[i].start();
34-
}
69+
for (Thread thread : threads) {
70+
thread.start();
71+
}
3572

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();
73+
for (Thread thread : threads) {
74+
try {
75+
thread.join();
76+
} catch (InterruptedException e) {
77+
e.printStackTrace();
78+
}
4279
}
80+
81+
long end = System.currentTimeMillis();
82+
83+
System.out.println(" - took " + (end - start) + "ms");
4384
}
4485
}
4586
}

src/labs/sem1/lab10/Stack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class Stack {
66
protected StackNode head;
7-
protected int size;
7+
protected int size = 0;
88

99
public void push(int i) {
1010
StackNode node = new StackNode(i);
@@ -13,7 +13,7 @@ public void push(int i) {
1313
size++;
1414
}
1515

16-
public int pop() throws RuntimeException {
16+
public int pop() {
1717
if (head == null) {
1818
throw new RuntimeException("Stack is empty");
1919
}
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
package labs.sem1.lab10;
22

3-
public class SynchroStack extends Stack {
3+
public class SynchroStack extends DebugStack {
44

55
public synchronized void push(int i) {
6-
Utils.printRow("START - Push");
76
super.push(i);
8-
Utils.printRow("END - Push");
97
}
108

119
public synchronized int pop() {
12-
Utils.printRow("START - Pop");
13-
int i = super.pop();
14-
Utils.printRow("END - Pop");
15-
return i;
10+
return super.pop();
1611
}
1712

1813
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;
14+
return super.equals(o);
2315
}
2416

2517
public synchronized String toString() {
26-
Utils.printRow("START - toString");
27-
String s = super.toString();
28-
Utils.printRow("END - toString");
29-
return s;
18+
return super.toString();
3019
}
3120
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package labs.sem1.lab10;
2+
3+
public class SynchroStackFast extends DebugStack {
4+
// с этим классом одновременно может работать один записывающий поток
5+
// (push и pop) и несколько читающих (equals и toString)
6+
7+
// для этого есть два объекта-монитора: для записи и для чтения
8+
// при записи блокируется монитор для записи, при чтении - монитор для чтения
9+
10+
Object readLock = new Object();
11+
Object writeLock = new Object();
12+
13+
public void push(int i) {
14+
synchronized (writeLock) {
15+
super.push(i);
16+
}
17+
}
18+
19+
public int pop() {
20+
synchronized (writeLock) {
21+
return super.pop();
22+
}
23+
}
24+
25+
public boolean equals(Object o) {
26+
synchronized (readLock) {
27+
return super.equals(o);
28+
}
29+
}
30+
31+
public String toString() {
32+
synchronized (readLock) {
33+
return super.toString();
34+
}
35+
}
36+
}
-22.7 KB
Binary file not shown.

src/labs/sem1/lab10/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package labs.sem1.lab10;
22

33
public class Utils {
4-
private static int threadsCount = 4;
4+
private static int threadsCount = 8;
55

66
public static void initCSV() {
77
// generate string with headers (e.g. "main,Thread-0,Thread-1,...")

src/test/stack/MainNew.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package test.stack;
2+
3+
import labs.sem1.lab10.SynchroStack;
4+
import labs.sem1.lab10.Utils;
5+
6+
public class MainNew {
7+
public static void mainOld(String[] args) {
8+
SynchroStack stack = new SynchroStack();
9+
Utils.initCSV();
10+
11+
for (int i = 0; i < 100; i++) {
12+
stack.push(i);
13+
}
14+
15+
Thread[] threads = new Thread[4];
16+
17+
for (int i = 0; i < 2; i++) {
18+
threads[i * 2] = new Thread(() -> {
19+
Utils.printRow("START - Stack 10 pushes");
20+
for (int j = 0; j < 10; j++) {
21+
stack.push(j);
22+
}
23+
Utils.printRow("END - Stack 10 pushes");
24+
});
25+
26+
threads[i * 2 + 1] = new Thread(() -> {
27+
Utils.printRow("START - Stack 10 pops");
28+
for (int j = 0; j < 10; j++) {
29+
stack.pop();
30+
}
31+
Utils.printRow("END - Stack 10 pops");
32+
});
33+
}
34+
35+
for (int i = 0; i < threads.length; i++) {
36+
threads[i].start();
37+
}
38+
39+
for (int i = 0; i < threads.length; i++) {
40+
try {
41+
threads[i].join();
42+
Utils.printRow("END - Thread " + i + " join");
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
}
48+
49+
private double benchmarkStack(Object o, Runnable r, int times) {
50+
// all methods available for class Stack
51+
Runnable[] methods = {
52+
() -> {
53+
for (int i = 0; i < times; i++) {
54+
o.push(i);
55+
}
56+
},
57+
() -> {
58+
for (int i = 0; i < times; i++) {
59+
o.pop();
60+
}
61+
},
62+
() -> {
63+
for (int i = 0; i < times; i++) {
64+
o.equals(o);
65+
}
66+
},
67+
() -> {
68+
for (int i = 0; i < times; i++) {
69+
o.toString();
70+
}
71+
}
72+
};
73+
}
74+
75+
private void testMultithreading(Object o) {
76+
// create list of 4 available methods for Stack class
77+
// create 2 threads for each method
78+
// with 10 iterations
79+
80+
// list of 4 methods
81+
Runnable[] methods = {
82+
() -> {
83+
for (int i = 0; i < 10; i++) {
84+
stack.push(i);
85+
}
86+
},
87+
() -> {
88+
for (int i = 0; i < 10; i++) {
89+
stack.pop();
90+
}
91+
},
92+
() -> {
93+
for (int i = 0; i < 10; i++) {
94+
stack.equals(stack);
95+
}
96+
},
97+
() -> {
98+
for (int i = 0; i < 10; i++) {
99+
stack.toString();
100+
}
101+
}
102+
};
103+
}
104+
105+
}

0 commit comments

Comments
 (0)