Sunday, 26 February 2017

LinkedBlockingQueue in Java Example program

Ma'lumki LinkedBlockingQueue JDK 1.5dan boshlab foydalaniladi.
"Queue" so'zi navbat("Очередь") ma'nosini bildiradi.  LinkedBlockingQueue java.util.concurrent paketiga tegishli. 
BlockingQueue ga element qo'shish uchun put, add yoki offer ishlatiladi. BlockingQueue dan birinchi turgan elementni "olish" uchun take(), poll() yoki remove() ishlatiladi.
Misollar:

Misol #1: BlockingQueue sinfi ob’ektini yaratish va unga qiymat berish

BlockingQueue<String> myQueue = new LinkedBlockingQueue<>(10);
try {

    for (int i=1;i<=10;i++)

        myQueue.put("m"+i);

} catch (InterruptedException e) {

    e.printStackTrace();

}
 
Misol #2: 1-misoldagi myQueue ob’ekti qiymatlarini 2ta thread(поток) yordamida navbatma-navbat o’qish.

Bunda Threadlar tezligi turlicha:

package linkedblockingqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/** * Created by Mansurjon on 2/16/2017. */
public class Sample3 {

    private static BlockingQueue<String> myQueue = new LinkedBlockingQueue<>(10);
    public static void main(String[] args) {
        try {
            for (int i=1;i<=10;i++)
                myQueue.put("m"+i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Our queue:"+ myQueue);
        new Thread(new Runnable() {
            @Override            public void run() {

                while (!myQueue.isEmpty())
                    try {
                        String data = myQueue.take();
                        System.out.println("Thread 1:"+data);
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

            }
        },"myThread1").start();

        new Thread(new Runnable() {
            @Override            public void run() {
                while (!myQueue.isEmpty())
                    try {
                        String data = myQueue.take();
                        System.out.println("Thread 2:"+data);
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

            }
        },"myThread2").start();
}}

-------------------------
Natija:
Our queue:[m1, m2, m3, m4, m5, m6, m7, m8, m9, m10]
Thread 1:m1
Thread 2:m2
Thread 1:m3
Thread 1:m4
Thread 1:m5
Thread 1:m6
Thread 1:m7
Thread 2:m8
Thread 1:m9
Thread 1:m10

2 comments:

  1. Replies
    1. Masalan, client-server texnologiyasida bazaga insert qilinishi lozim bo'lgan obyektlarni Queue da saqlab uni insert qilgan yaxshi. Agar nimadir sabab bilan baza bn aloqa yoqlosa(server o'chib qolsa va hk.) Queue da insert qilinishi lozim bgan obyektlar yigilaveradi, baza bilan aloqa tiklangach qolgan joyidan insert qilish davom etaveradi

      Delete

Note: only a member of this blog may post a comment.

Tasks and Threads

  Differences Between Task And Thread: 1. The Thread class is used for creating and manipulating a thread in Windows.  2. A Task represents ...