FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/lowlevel/SynchronizedComparison.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
lowlevel
/
SynchronizedComparison.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
87 lines (82 loc) · 2.01 KB
Breadcrumbs
OnJava8-Examples
/
lowlevel
/
SynchronizedComparison.java
Copy path
File metadata and controls
87 lines (82 loc) · 2.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
77
78
79
80
81
82
83
84
85
86
87
// lowlevel/SynchronizedComparison.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Synchronizing blocks instead of entire methods
// speeds up access.
import
java
.
util
.*;
import
java
.
util
.
stream
.*;
import
java
.
util
.
concurrent
.*;
import
java
.
util
.
concurrent
.
atomic
.*;
import
onjava
.
Nap
;
abstract
class
Guarded
{
AtomicLong
callCount
=
new
AtomicLong
();
public
abstract
void
method
();
@
Override
public
String
toString
() {
return
getClass
().
getSimpleName
() +
": "
+
callCount
.
get
();
}
}
class
SynchronizedMethod
extends
Guarded
{
@
Override
public
synchronized
void
method
() {
new
Nap
(
0.01
);
callCount
.
incrementAndGet
();
}
}
class
CriticalSection
extends
Guarded
{
@
Override
public
void
method
() {
new
Nap
(
0.01
);
synchronized
(
this
) {
callCount
.
incrementAndGet
();
}
}
}
class
Caller
implements
Runnable
{
private
Guarded
g
;
Caller
(
Guarded
g
) {
this
.
g
=
g
; }
private
AtomicLong
successfulCalls
=
new
AtomicLong
();
private
AtomicBoolean
stop
=
new
AtomicBoolean
(
false
);
@
Override
public
void
run
() {
new
Timer
().
schedule
(
new
TimerTask
() {
public
void
run
() {
stop
.
set
(
true
); }
},
2500
);
while
(!
stop
.
get
()) {
g
.
method
();
successfulCalls
.
getAndIncrement
();
}
System
.
out
.
println
(
"-> "
+
successfulCalls
.
get
());
}
}
public
class
SynchronizedComparison
{
static
void
test
(
Guarded
g
) {
List
<
CompletableFuture
<
Void
>>
callers
=
Stream
.
of
(
new
Caller
(
g
),
new
Caller
(
g
),
new
Caller
(
g
),
new
Caller
(
g
))
.
map
(
CompletableFuture
::
runAsync
)
.
collect
(
Collectors
.
toList
());
callers
.
forEach
(
CompletableFuture
::
join
);
System
.
out
.
println
(
g
);
}
public
static
void
main
(
String
[]
args
) {
test
(
new
CriticalSection
());
test
(
new
SynchronizedMethod
());
}
}
/* Output:
-> 159
-> 159
-> 159
-> 159
CriticalSection: 636
-> 65
-> 21
-> 11
-> 68
SynchronizedMethod: 165
*/
Back
|
FazBrowse Home
|
New Git URL