Skip to content

Commit a60761e

Browse files
committed
Initial commit
0 parents  commit a60761e

File tree

10 files changed

+230
-0
lines changed

10 files changed

+230
-0
lines changed

.gitignore

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff
5+
.idea/**/workspace.xml
6+
.idea/**/tasks.xml
7+
.idea/**/usage.statistics.xml
8+
.idea/**/dictionaries
9+
.idea/**/shelf
10+
11+
# Generated files
12+
.idea/**/contentModel.xml
13+
14+
# Sensitive or high-churn files
15+
.idea/**/dataSources/
16+
.idea/**/dataSources.ids
17+
.idea/**/dataSources.local.xml
18+
.idea/**/sqlDataSources.xml
19+
.idea/**/dynamic.xml
20+
.idea/**/uiDesigner.xml
21+
.idea/**/dbnavigator.xml
22+
23+
# Gradle
24+
.idea/**/gradle.xml
25+
.idea/**/libraries
26+
27+
# Gradle and Maven with auto-import
28+
# When using Gradle or Maven with auto-import, you should exclude module files,
29+
# since they will be recreated, and may cause churn. Uncomment if using
30+
# auto-import.
31+
# .idea/artifacts
32+
# .idea/compiler.xml
33+
# .idea/jarRepositories.xml
34+
# .idea/modules.xml
35+
# .idea/*.iml
36+
# .idea/modules
37+
# *.iml
38+
# *.ipr
39+
40+
# CMake
41+
cmake-build-*/
42+
43+
# Mongo Explorer plugin
44+
.idea/**/mongoSettings.xml
45+
46+
# File-based project format
47+
*.iws
48+
49+
# IntelliJ
50+
out/
51+
52+
# mpeltonen/sbt-idea plugin
53+
.idea_modules/
54+
55+
# JIRA plugin
56+
atlassian-ide-plugin.xml
57+
58+
# Cursive Clojure plugin
59+
.idea/replstate.xml
60+
61+
# Crashlytics plugin (for Android Studio and IntelliJ)
62+
com_crashlytics_export_strings.xml
63+
crashlytics.properties
64+
crashlytics-build.properties
65+
fabric.properties
66+
67+
# Editor-based Rest Client
68+
.idea/httpRequests
69+
70+
# Android studio 3.1+ serialized cache file
71+
.idea/caches/build_file_checksums.ser

.idea/description.html

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blocking-queue.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Blocking Queue Implementation in Java
2+
3+
A blocking queue is defined as a queue which blocks the caller of the enqueue method if there's no more capacity to add the new item being enqueued.
4+
Similarly, the queue blocks the dequeue caller if there are no items in the queue.
5+
Also, the queue notifies a blocked enqueuing thread when space becomes available and a blocked dequeuing thread when an item becomes available in the queue.

src/com/learning/BlockingQueue.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.learning;
2+
3+
public class BlockingQueue<T> {
4+
5+
final Object lock = new Object();
6+
7+
T[] array;
8+
int size = 0;
9+
int capacity;
10+
int head = 0;
11+
int tail = 0;
12+
13+
@SuppressWarnings("unchecked")
14+
public BlockingQueue(int capacity) {
15+
this.capacity = capacity;
16+
array = (T[]) new Object[capacity];
17+
}
18+
19+
public void enqueue(T item) throws InterruptedException {
20+
synchronized (lock) {
21+
// wait for the queue to have space
22+
while (size == capacity) {
23+
lock.wait();
24+
}
25+
26+
// reset tail to the beginning if the tail is already
27+
// at the end of the backing array
28+
if (tail == capacity) {
29+
tail = 0;
30+
}
31+
32+
// place the item in the tail
33+
array[tail] = item;
34+
size++;
35+
tail++;
36+
37+
// don't forget to notify any other threads waiting on
38+
// a change in value of size. There might be consumers
39+
// waiting for the queue to have at least one element
40+
lock.notifyAll();
41+
}
42+
}
43+
44+
public T dequeue() throws InterruptedException {
45+
T item = null;
46+
47+
synchronized (lock) {
48+
// wait for at least one item to be enqueued
49+
while (size == 0) {
50+
lock.wait();
51+
}
52+
53+
// reset head to start of array if its past the array
54+
if (head == capacity) {
55+
head = 0;
56+
}
57+
58+
// store the reference to the object being dequeued
59+
// and overwrite with null
60+
item = array[head];
61+
array[head] = null;
62+
head++;
63+
size--;
64+
65+
// don't forget to call notify, there might be another thread
66+
// blocked in the enqueue method.
67+
lock.notifyAll();
68+
}
69+
70+
return item;
71+
}
72+
}

src/com/learning/Main.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.learning;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) throws InterruptedException {
6+
final BlockingQueue<Integer> queue = new BlockingQueue<>(5);
7+
8+
Thread t1 = new Thread(() -> {
9+
try {
10+
for (int i = 0; i < 25; i++) {
11+
queue.enqueue(i);
12+
System.out.println("Enqueued " + i);
13+
}
14+
} catch (InterruptedException ignored) {}
15+
});
16+
17+
Thread t2 = new Thread(() -> {
18+
try {
19+
for (int i = 0; i < 25; i++) {
20+
System.out.println("Thread 2 dequeued: " + queue.dequeue());
21+
}
22+
} catch (InterruptedException ignored) { }
23+
});
24+
25+
Thread t3 = new Thread(() -> {
26+
try {
27+
for (int i = 0; i < 25; i++) {
28+
System.out.println("Thread 3 dequeued: " + queue.dequeue());
29+
}
30+
} catch (InterruptedException ignored) { }
31+
});
32+
33+
t1.start();
34+
Thread.sleep(4000);
35+
t2.start();
36+
37+
t2.join();
38+
39+
t3.start();
40+
t1.join();
41+
t3.join();
42+
}
43+
}

0 commit comments

Comments
 (0)