簡體   English   中英

Java-使用兩個線程進行線程間通信來打印數字序列

[英]Java - print sequence of numbers using two threads with inter thread communication

我想以以下格式打印號碼。 這應該通過兩個線程t1,t2來注意。 任何人都可以幫助增強我編寫的以下代碼嗎?

First t1 should print 0-4
Then t2 should print 5-9
Then again t1 should print 10-14
Then t2 should print 15-19

0 1 2 3 4

5 6 7 8 9

10 11 12 13 14

class PrintNumber implements Runnable{
    String name;

    public void run(){
            print();
    }

    synchronized public void print(){
            for(int i=0;i< 5;i++){
                System.out.println(i+" -- "+Thread.currentThread());
            }
    }

    public static void main(String[] args){
        Runnable r = new PrintNumber();
        Thread t1 = new Thread(r,"t1");
        Thread t2 = new Thread(r,"t2");
        t1.start();
        t2.start();
    }
}

可以使用兩個Sempaphores來代替使用低級的wait()notify() 每個Runnable都有一個等待Semaphore ,並使用一個Semaphore通知下一個Semaphore

import java.util.concurrent.Semaphore;

class PrintNumber implements Runnable{

    static volatile int nextStartIdx; 

    private Semaphore waitForSemaphore;

    private Semaphore next;


    public PrintNumber(Semaphore waitFor, Semaphore next) {
        this.waitForSemaphore = waitFor; 
        this.next = next;
    }


    public void run(){
        while (true) {
            print();
        }

    }

    public void print() {
        try {
            waitForSemaphore.acquire();
            int start = nextStartIdx;

            for(int i=0;i< 5;i++){
                System.out.println(String.format("%d -- %s", i + start, Thread.currentThread().getName()));
            }           

            nextStartIdx += 5;
            next.release();

        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args){

        Semaphore a = new Semaphore(1);
        Semaphore b = new Semaphore(0);
        Thread t1 = new Thread(new PrintNumber(a,b),"t1");
        Thread t2 = new Thread(new PrintNumber(b,a),"t2");
        t1.start();
        t2.start();
    }
}

您可以使用wait和notify來實現sczenario的線程間通信。

class PrintNumber implements Runnable {
    String name;
    Integer count = 0;

    @Override
    public void run() {
        try {
            print();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public void print() throws InterruptedException {
        while (count < 15) {
            for (int i = 0; i < 5; i++) {
                count++;
                System.out.println(count + " -- " + Thread.currentThread());
            }
            notifyAll();
            wait();
        }
    }

    public static void main(final String[] args) {
        final Runnable r = new PrintNumber();
        final Thread t1 = new Thread(r, "t1");
        final Thread t2 = new Thread(r, "t2");
        t1.start();
        t2.start();
    }
}

有關更多信息,請參見:

Java中使用wait()和notify()的簡單場景

暫無
暫無

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

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