Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP-03-Synchronization
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamadbashar.disoki
PP-03-Synchronization
Commits
05721e19
Commit
05721e19
authored
Nov 15, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Producer Consumer Example
parent
172ae79c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
92 additions
and
1 deletion
+92
-1
README.md
README.md
+3
-1
Consumer.java
src/main/java/ProducerConsumer/Consumer.java
+16
-0
EventStorage.java
src/main/java/ProducerConsumer/EventStorage.java
+41
-0
Main.java
src/main/java/ProducerConsumer/Main.java
+17
-0
Producer.java
src/main/java/ProducerConsumer/Producer.java
+15
-0
No files found.
README.md
View file @
05721e19
...
...
@@ -5,7 +5,9 @@
*
Race Condition
*
Critical Section
*
Memory inconsistency
*
*
ParkingCash Example
*
ProducerConsumer Example
*
synchronized, wait, notify, notifyAll keyword
*
Locks and Atomic Variables
*Useful Links*
...
...
src/main/java/ProducerConsumer/Consumer.java
0 → 100644
View file @
05721e19
package
ProducerConsumer
;
public
class
Consumer
implements
Runnable
{
private
EventStorage
storage
;
public
Consumer
(
EventStorage
storage
)
{
this
.
storage
=
storage
;
}
@Override
public
void
run
()
{
for
(
int
i
=
0
;
i
<
100
;
i
++){
storage
.
get
();
}
}
}
src/main/java/ProducerConsumer/EventStorage.java
0 → 100644
View file @
05721e19
package
ProducerConsumer
;
import
java.util.Date
;
import
java.util.LinkedList
;
import
java.util.Queue
;
public
class
EventStorage
{
private
int
maxSize
;
private
Queue
<
Date
>
storage
;
public
EventStorage
()
{
maxSize
=
10
;
storage
=
new
LinkedList
<>();
}
public
synchronized
void
set
(){
while
(
storage
.
size
()==
maxSize
){
try
{
wait
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
storage
.
offer
(
new
Date
());
System
.
out
.
printf
(
"Set: %d"
,
storage
.
size
());
notify
();
}
public
synchronized
void
get
(){
while
(
storage
.
size
()==
0
){
try
{
wait
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
String
element
=
storage
.
poll
().
toString
();
System
.
out
.
printf
(
"Get: %d: %s\n"
,
storage
.
size
(),
element
);
notify
();
}
}
src/main/java/ProducerConsumer/Main.java
0 → 100644
View file @
05721e19
package
ProducerConsumer
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
EventStorage
storage
=
new
EventStorage
();
Producer
producer
=
new
Producer
(
storage
);
Thread
thread1
=
new
Thread
(
producer
);
Consumer
consumer
=
new
Consumer
(
storage
);
Thread
thread2
=
new
Thread
(
consumer
);
thread2
.
start
();
thread1
.
start
();
}
}
src/main/java/ProducerConsumer/Producer.java
0 → 100644
View file @
05721e19
package
ProducerConsumer
;
public
class
Producer
implements
Runnable
{
private
EventStorage
storage
;
public
Producer
(
EventStorage
storage
){
this
.
storage
=
storage
;
}
@Override
public
void
run
()
{
for
(
int
i
=
0
;
i
<
100
;
i
++){
storage
.
set
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment