簡體   English   中英

如何交替調用Java中的三個線程

[英]How to call three threads in Java alternatively

給定數字 1、2、3、4、5、6、7、8、9、10、11、12 我有三個線程,比如 Thread-1(print 1)、Thread-2(print 2) 和 Thread- 3(打印 3)然后再次 hread-1(打印 4)、Thread-2(打印 5)和 Thread-3(打印 6)等等。我如何在循環中調用它們以便它們交替執行,輸出(預期)--- 1 2 3 4 5 6 7 8 9 10 11 12 但是我下面的代碼只打印 1 2 3

public class RunAlternateThread {

    public static void main(String[] args) throws InterruptedException {
        PrintNum count = new PrintNum();
        Thread t1 = new Thread(new Thread1(count));
        //t1.setPriority(5);
        Thread t2 = new Thread(new Thread2(count));
        //t2.setPriority(7);
        Thread t3 = new Thread(new Thread3(count));
        //t3.setPriority(9);

        t1.start();
        t2.start();
        t3.start();

    }
}

class Thread1 implements Runnable {
    PrintNum count = null;
    Thread1(PrintNum count) {
        this.count = count;
    }

    public void run() {
        synchronized (count) {
                count.print1();
                    //count.wait();
            count.notifyAll();
            try {
                count.wait(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }            }
        }
}

class Thread2 implements Runnable {
    PrintNum count = null;

    Thread2(PrintNum count) {
        this.count = count;
    }

    public void run() {
            synchronized (count) {
                count.print2();
                count.notifyAll();
                try {
                    count.wait(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
    }
}
class Thread3 implements Runnable {
    PrintNum count = null;

    Thread3(PrintNum count) {
        this.count = count;
    }

    public void run() {
        synchronized (count) {
            count.print3();
            count.notifyAll();
            try {
                count.wait(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
class PrintNum {
    static int num = 1;
    void print1() {
        if(num == 12) {
            try {
                Thread.sleep(10000);
            }
            catch (Exception e){

            }
        }
        System.out.println(this.num);
        num++;
    }

    void print2() {
        if(num == 12) {
            try {
                Thread.sleep(10000);
            }
            catch (Exception e){

            }
        }
        System.out.println(this.num);
        num++;
    }
    void print3() {
        if(num == 12) {
            try {
                Thread.sleep(10000);
            }
            catch (Exception e){

            }
        }
        System.out.println(this.num);
        num++;
    }
}


這就是你要問的,我留下了線程號,這樣你就可以測試它

public class Test {
   public static void main(String[] args) {
       PrintNum count = new PrintNum();
       Thread t1 = new MyThread(count, 1);
       Thread t2 = new MyThread(count, 2);
       Thread t3 = new MyThread(count, 3);
       t1.start();
       t2.start();
       t3.start();
   }
}

class MyThread extends Thread implements Runnable {
    final PrintNum count;
    final int threadNum;
    MyThread(PrintNum count, int threadNum) {
        this.count = count;
        this.threadNum = threadNum;
    }
    public void run() {
        while (count.num < 12) {
             synchronized (count) {
                 count.print(this.threadNum);
             }
        }
    }
}
class PrintNum {
    int num = 0;
    void print(int tNum) {
        try {
            this.num++;
            System.out.println(this.num + " Thread: " + tNum);
            this.notifyAll();
            this.wait();
            if(num == 12) this.notifyAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

暫無
暫無

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

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