-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Created offer entry for java queue interface #7407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
13ef949
Created offer entry for java queue interface
arisdelacruz 5088b54
Update offer.md
arisdelacruz b1f9b97
content fixes
mamtawardhani f8104b9
Merge branch 'main' into offer
mamtawardhani 99dac8d
Update content/java/concepts/queue/terms/offer/offer.md
avdhoottt 1599895
Update content/java/concepts/queue/terms/offer/offer.md
avdhoottt 4df022e
Update content/java/concepts/queue/terms/offer/offer.md
avdhoottt bc12624
Update content/java/concepts/queue/terms/offer/offer.md
avdhoottt c90f31f
Update content/java/concepts/queue/terms/offer/offer.md
avdhoottt e4ed5df
Merge branch 'main' into offer
avdhoottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
Title: 'offer()' | ||
Description: 'Inserts the specified element into the queue if possible without violating capacity limits.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Elements' | ||
- 'Queues' | ||
CatalogContent: | ||
- 'learn-java' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`.offer()`** method is part of the Java `Queue` interface and inserts an element at the end of the queue. It returns `true` if the element was added successfully, or `false` if the queue is at capacity and cannot accept new elements. | ||
|
||
Unlike the `add()` method, which throws an exception when the queue is full, `offer()` fails gracefully, making it useful when there is a need to handle capacity limits without triggering exceptions. | ||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Syntax | ||
|
||
```pseudo | ||
queue.offer(element) | ||
``` | ||
|
||
**Parameters:** | ||
|
||
- `element`: The element to be inserted to the queue. | ||
|
||
**Return value:** | ||
|
||
- Returns `true` if the element was added successfully, `false` if the queue is full. | ||
|
||
The `offer()` method is non-blocking, meaning it returns immediately if the queue is full. Instead of throwing an exception when capacity is reached, it returns `false`, making it safer for capacity-aware operations. Its behavior may vary between bounded and unbounded queue implementations. | ||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Example: Using `offer()` in a Print Job Queue System | ||
|
||
In this example, a bounded queue with capacity two demonstrates how `offer()` returns `true` when adding elements within capacity and `false` when the queue is full: | ||
avdhoottt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```java | ||
import java.util.Queue; | ||
import java.util.concurrent.ArrayBlockingQueue; | ||
|
||
public class PrintJobQueue { | ||
public static void main(String[] args) { | ||
Queue<String> printQueue = new ArrayBlockingQueue<>(2); // capacity of 2 | ||
|
||
boolean added = printQueue.offer("Document1"); | ||
System.out.println("Added Document1: " + added); | ||
|
||
added = printQueue.offer("Document2"); | ||
System.out.println("Added Document2: " + added); | ||
|
||
// Attempt to add another job to a full queue | ||
added = printQueue.offer("Document3"); | ||
System.out.println("Added Document3: " + added); | ||
} | ||
} | ||
``` | ||
|
||
The output generated by this code is: | ||
|
||
```shell | ||
Added Document1: true | ||
Added Document2: true | ||
Added Document3: false | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.