Skip to content

Commit 4485357

Browse files
committed
Refactoring.
1 parent 661124b commit 4485357

File tree

5 files changed

+50
-148
lines changed

5 files changed

+50
-148
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package by.bntu.fitr.povt.multithreadingCommunication.threadLocal.example3;
2+
3+
/**
4+
* @author Alexey Druzik on 25.08.2020
5+
*/
6+
public class BuilderWithThreadLocal {
7+
8+
private ThreadLocal<Integer> counter = new ThreadLocal<Integer>();
9+
10+
public void build() {
11+
System.out.println("Thread " + Thread.currentThread().getName() + " Build some structure...");
12+
if (counter.get() == null)
13+
counter.set(0);
14+
15+
counter.set(counter.get() + 1);
16+
try {
17+
Thread.sleep(100);
18+
}
19+
catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
24+
public int getCount() {
25+
return counter.get();
26+
}
27+
28+
}

src/by/bntu/fitr/povt/multithreadingCommunication/threadLocal/example3/SomeBuilder.java renamed to src/by/bntu/fitr/povt/multithreadingCommunication/threadLocal/example3/BuilderWithoutThreadLocal.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
/**
44
* @author Alexey Druzik on 25.08.2020
55
*/
6-
public class SomeBuilder {
6+
public class BuilderWithoutThreadLocal {
7+
78
private int counter;
9+
810
public void build() {
911
System.out.println("Thread " + Thread.currentThread().getName() + " Build some structure...");
1012
counter++;
@@ -19,4 +21,5 @@ public void build() {
1921
public int getCount() {
2022
return counter;
2123
}
22-
}
24+
25+
}

src/by/bntu/fitr/povt/multithreadingCommunication/threadLocal/example3/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
public class Main {
88
public static void main(String[] args) throws InterruptedException {
99

10-
SomeBuilder builder = new SomeBuilder();
11-
new SomeBuilderDemo2(builder);
12-
new SomeBuilderDemo2(builder);
10+
BuilderWithThreadLocal builder = new BuilderWithThreadLocal();
11+
new SomeBuilderDemo(builder);
12+
new SomeBuilderDemo(builder);
1313

1414
}
1515
}

src/by/bntu/fitr/povt/multithreadingCommunication/threadLocal/example3/SomeBuilderDemo.java

Lines changed: 14 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3,127 +3,28 @@
33
/**
44
* @author Alexey Druzik on 25.08.2020
55
*/
6-
public class SomeBuilderDemo {
6+
public class SomeBuilderDemo implements Runnable {
77

8+
private Thread thread;
9+
private BuilderWithThreadLocal builder;
810

9-
10-
public static class SomeBuilder {
11-
12-
13-
14-
private ThreadLocal<Integer> counter = new ThreadLocal<Integer>();
15-
16-
17-
18-
public void build() {
19-
20-
System.out.println("Thread " + Thread.currentThread().getName() + " Build some structure...");
21-
22-
23-
24-
if (counter.get() == null)
25-
26-
counter.set(0);
27-
28-
29-
30-
counter.set(counter.get() + 1);
31-
32-
33-
34-
try {
35-
36-
Thread.sleep(100);
37-
38-
}
39-
40-
catch (InterruptedException e) {
41-
42-
e.printStackTrace();
43-
44-
}
45-
46-
}
47-
48-
49-
50-
public int getCount() {
51-
52-
return counter.get();
53-
54-
}
55-
11+
public SomeBuilderDemo(BuilderWithThreadLocal builder) throws InterruptedException {
12+
this.builder = builder;
13+
thread = new Thread(this);
14+
thread.start();
15+
thread.join();
5616
}
5717

58-
59-
60-
public static class SomeBuilderThread extends Thread {
61-
62-
63-
64-
private SomeBuilder builder;
65-
66-
67-
68-
public SomeBuilderThread(SomeBuilder builder) {
69-
70-
this.builder = builder;
71-
72-
}
73-
74-
75-
76-
@Override
77-
78-
public void run() {
79-
80-
for (int i = 0; i < Math.random() * 10; i++) {
81-
82-
builder.build();
83-
84-
}
85-
86-
System.out.println("My name is " + getName() + " and I built " + builder.getCount() + " things");
87-
88-
}
89-
90-
}
91-
92-
93-
94-
public static void main(String[] args) {
95-
96-
SomeBuilder builder = new SomeBuilder();
97-
98-
99-
100-
Thread thread1 = new SomeBuilderThread(builder);
101-
102-
Thread thread2 = new SomeBuilderThread(builder);
103-
104-
try {
105-
106-
thread1.start();
107-
108-
thread2.start();
109-
110-
111-
112-
thread1.join();
113-
114-
thread2.join();
115-
18+
@Override
19+
public void run() {
20+
for (int i = 0; i < Math.random() * 10; i++) {
21+
builder.build();
11622
}
117-
118-
catch (InterruptedException e) {
119-
120-
e.printStackTrace();
121-
122-
}
123-
23+
System.out.println("My name is " + thread.getName() + " and I built " + builder.getCount() + " things");
12424
}
12525

12626
}
12727

12828

12929

30+

src/by/bntu/fitr/povt/multithreadingCommunication/threadLocal/example3/SomeBuilderDemo2.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)