English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

تعليميات Java الأساسية

تحكم Java في العملية

Java Array (المحفظة)

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

معالجة الاستثناءات Java

Java List (القائمة)

Java Queue (الطابور)

مجموعات Java Map

مجموعات Java Set

Java إدخال/إخراج (I/O)

Java Reader/Writer

مواضيع أخرى Java

Java BlockingQueue

في هذا الدليل، سنتعلم واجهة Java BlockingQueue وما هي طرقها.

The Java Collections framework's BlockingQueue interface extends the Queue interface. It allows any operation to wait until it is successfully executed.

For example, if we want to delete an element from an empty queue, the blocking queue allows the deletion operation to wait until there are some elements to be deleted in the queue.

Classes that implement BlockingQueue

Since BlockingQueue is an interface, we cannot provide its direct implementation.

To use the functions of BlockingQueue, we need to use the class that implements it.

How to use a blocking queue?

We must import the java.util.concurrent.BlockingQueue package to use BlockingQueue.

//Array implementation of BlockingQueue
BlockingQueue<String> animal1 = new ArraryBlockingQueue<>();
//LinkedList implementation of BlockingQueue
BlockingQueue<String> animal2 = new LinkedBlockingQueue<>();

In this case, we have created objects animal1 and animal2 of the classes ArrayBlockingQueue and LinkedBlockingQueue respectively. These objects can use the functions of the BlockingQueue interface.

BlockingQueue methods

According to whether the queue is full or empty, the methods of the blocking queue can be divided into 3 categories:

methods that throw exceptions

  • add() - Inserts the element into the end of the blocking queue. If the queue is full, it throws an exception.

  • element() - Returns the head of the blocking queue. If the queue is empty, it throws an exception.

  • remove() - Removes an element from the blocking queue. If the queue is empty, it throws an exception.

methods that return a value

  • offer() - Inserts the specified element into the end of the blocking queue. If the queue is full, it returns false.

  • peek() - Returns the head of the blocking queue. If the queue is empty, it returns null.

  • poll() - Removes an element from the blocking queue. If the queue is empty, it returns null.

offer() and poll()more content

methods offer() and poll() can be used with timeout. That is, we can pass the time unit as a parameter. For example,

offer(value, 100, milliseconds)}

هنا،

  • value هو العنصر الذي نريد إدراجه في الخط الأمامي

  • وسنضبط وقت الصلاحية على 100 ميليسي ثانية

يعني أن طريقة offer() ستجرب إدراج عنصر في الخط الأمامي لمدة 100 ميليسي ثانية. إذا لم يتم إدراج العنصر في غضون 100 ميليسي ثانية، فإن الطريقة ستعود بـ false.

ملاحظة:بالإضافة إلى الميليسي ثانية، يمكننا استخدام الوحدات الزمنية التالية في طرق offer() وpoll(): أيام، ساعات، دقائق، ثواني، ميكرو ثانية ونانوثانية.

طرق التشغيل BlockingQueue

يقدم BlockingQueue أيضًا بعض الطرق للتعامل مع العمليات المتوقفة والانتظار، إذا كان الخط الأمامي ممتلئًا أو فارغًا.

  • put() - إدراج العنصر في الخط الأمامي. إذا كان الخط الأمامي ممتلئًا، فإنه سينتظر حتى يكون هناك مساحة لإدراج العنصر.

  • take() - إزالة العنصر وإرجاعه من الخط الأمامي. إذا كان الخط الأمامي فارغًا، فإنه سينتظر حتى يكون هناك عنصر لإزالته.

افترض، نريد إدراج عنصر في الخط الأمامي. إذا كان الخط الأمامي ممتلئًا، فإن طريقة put() ستنتظر حتى يكون هناك مساحة في الخط الأمامي لإدراج العنصر.

على سبيل المثال، إذا أردنا إزالة عنصر من الخط الأمامي. إذا كان الخط الأمامي فارغًا، فإن طريقة take() ستنتظر حتى يحتوي الخط الأمامي على العنصر الذي نريد إزالته.

تحقيق BlockingQueue في ArrayBlockingQueue

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
class Main {
    public static void main(String[] args) {
      //استخدام ArrayBlockingQueue لإنشاء خط أمامي معطوب
      BlockingQueue<Integer> numbers = new ArrayBlockingQueue<>(5);
      try {
        //إدراج العنصر في الخط الأمامي
        numbers.put(2);
        numbers.put(1);
        numbers.put(3);
        System.out.println("BLockingQueue: " + numbers);
        //من إزالة العنصر من الخط الأمامي
        int removedNumber = numbers.take();
        System.out.println("عدد تم إزالته: " + removedNumber);
      }
      catch(Exception e) {
          e.getStackTrace();
      }
    }
}

نتائج الإخراج

BlockingQueue: [2, 1, 3]
عنصر تم إزالته: 2

للحصول على معلومات إضافية حول ArrayBlockingQueue، يرجى زيارةJava ArrayBlockingQueue.

لماذا اختيار BlockingQueue؟

في Java، يُعتبر BlockingQueue كـآمنة للنواةالجمعيات. هذا لأنها قد تساعد في العمليات المتعددة النواة.

افترض أن نواة واحدة تقوم بإدراج عنصر في القائمة، بينما تقوم نواة أخرى بإزالة العنصر من القائمة.

الآن، إذا كانت سرعة تنفيذ أول نواة بطيئة، يمكن أن تمنع قائمة الحجب النواة الثانية من الانتظار حتى اكتمال عملية النواة الأولى.