簡體   English   中英

使用 7 個線程生成無限序列

[英]Generating infinite sequence of numbers using 7 threads

我正在嘗試使用 Java 中的不同線程生成無限的數字序列。 這是我的代碼:

import java.util.*;

public class infinite_Streams {
    volatile boolean keepGenerating = true;
    volatile ArrayList<Integer> s1 = new ArrayList<>();
    Object lock1 = new Object();
    Random random = new Random();
    ArrayList<Object> obj1 = new ArrayList<>();


    void  generateInfiniteStream() {

        synchronized (lock1) {

        //BLOCK 1
            while(keepGenerating) {
                Object temp = Thread.currentThread().getId();
                System.out.println("Thread is : " + temp);
                s1.add(random.nextInt(11));
            }
            }
            //BLOCK 1 ENDS

        //BLOCK 2

//            for (int i = 0; i < 100000; i++) {
//                Object temp = Thread.currentThread().getId();
//                System.out.println("Thread is : " + temp);
//                s1.add(random.nextInt(11));
//            }
        //BLOCK 2 ENDS
    }

    void generateThreads(int num_threads){
        Thread[] threads = new Thread[num_threads];

        for(int i = 0 ; i < num_threads ; i++){
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    generateInfiniteStream();
                }
            });
        }

        for (Thread thread : threads) {

            thread.start();
        }


        try{
            for (Thread thread : threads) {
                thread.join();
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
    public void shutdown(){
        keepGenerating = false;
    }


    public static void main(String [] args){
        List<Integer> l1 = new ArrayList<>();
        int num_threads = 7;
        Scanner scan1 = new Scanner(System.in);
        infinite_Streams is1 = new infinite_Streams();

        is1.generateThreads(num_threads);
        System.out.println("Press any key to interrupt");
        Scanner scan2 = new Scanner(System.in);
        scan2.nextLine();
        is1.shutdown();
    }
}

代碼似乎確實生成了我需要的東西。 但我也有興趣了解如何涉及不同的線程。要查看我打印的內容(可以在\\\\BLOCK1看到)

Object temp = Thread.currentThread().getId();
 System.out.println("Thread is : " + temp);

問題是我只看到一個特定的線程,比如12 ,所以輸出看起來像Thread is : 12 always 。 另一方面,如果我注釋\\\\BLOCK1代碼,而是運行\\\\BLOCK2代碼,它生成有限數量的數字,我會在輸出中看到不同的線程編號,正如預期的那樣。 就像Thread is : 12 Thread is : 12 Thread is : 14等等。有人可以解釋一下,為什么我在生成無限數時沒有看到不同的線程數?

線程之一獲得鎖。 然后它會執行while(keepGenerating)直到您更改此變量。 所有其他線程都在等待,直到鎖空閑。 當您將keepGenerating設置為false 時,正在運行的線程將完成循環。 其他線程之一獲得鎖。 但是此時keepGenerating已經為false,所以不執行這個循環,直接退出。 然后下一個線程獲得鎖並再次看到沒有什么可做的。 等等。

所以實際上只有一個線程在生成隨機數。

如果你希望每個線程都生成隨機數,你應該同步塊之外使用while(keepGenerating) ,而不是之外,如下所示:

while(keepGenerating) {
    synchronized (lock1) {
        Object temp = Thread.currentThread().getId();
        System.out.println("Thread is : " + temp);
        s1.add(random.nextInt(11));
    }
}

然后每個線程將不會永遠獲得鎖定,而只是為了數字生成和輸出的單次執行。 然后鎖將被釋放,其他線程可以得到它,等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM