Java多個線程順序打印數字

要求

啓動N個線程, 這N個線程要不間斷按順序打印數字1-N. 將問題簡化爲3個線程無限循環打印1到3java

方法一: 使用synchronized

三個線程無序競爭同步鎖, 若是趕上的是本身的數字, 就打印. 這種方式會浪費大量的循環bash

public class TestSequential1 {
    private volatile int pos = 1;
    private volatile int count = 0;

    public void one(int i) {
        synchronized (this) {
            if (pos == i) {
                System.out.println("T-" + i + " " + count);
                pos = i % 3 + 1;
                count = 0;
            } else {
                count++;
            }
        }
    }

    public static void main(String[] args) {
        TestSequential1 demo = new TestSequential1();
        for (int i = 1; i <=3; i++) {
            int j = i;
            new Thread(()->{
                while(true) {
                    demo.one(j);
                }
            }).start();
        }
    }
}

輸出this

T-1 0
T-2 5793
T-3 5285
T-1 2616
T-2 33
T-3 28
T-1 22
T-2 44
T-3 6
T-1 881
T-2 118358
T-3 247380
T-1 30803
T-2 29627
T-3 52044
...

 

方法二: 使用synchronized配合wait()和notifyAll()

競爭同步鎖時使用wait()和notifyAll(), 能夠避免浪費循環線程

public class TestSequential4 {
    private volatile int pos = 1;
    private volatile int count = 0;
    private final Object obj = new Object();

    public void one(int i) {
        System.out.println(i + " try");
        synchronized (obj) {
            System.out.println(i + " in");
            try {
                while (pos != i) {
                    count++;
                    System.out.println(i + " wait");
                    obj.wait();
                }
                System.out.println("T-" + i + " " + count);
                pos = i % 3 + 1;
                count = 0;
                obj.notifyAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        TestSequential4 demo = new TestSequential4();
        for (int i = 3; i >=1; i--) {
            int j = i;
            new Thread(()->{
                while(true) {
                    demo.one(j);
                }
            }).start();
        }
    }
}

輸出blog

3 try
3 in
3 wait
2 try
2 in
2 wait
1 try
1 in
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
1 try
1 in
1 wait
T-2 1
2 try
2 in
2 wait
T-3 1
3 try
3 in
3 wait
2 wait
T-1 2
...

.同步

方法三: 使用可重入鎖

用Lock作, 非公平鎖, 三個線程競爭, 若是趕上的是本身的數字, 就打印. 這種方式也會浪費大量的循環it

public class TestSequential2 {
    private final Lock lock = new ReentrantLock();
    private volatile int pos = 1;
    private volatile int count = 0;

    public void one(int i) {
        lock.lock();
        if (pos == i) {
            System.out.println("T-" + i + " " + count);
            pos = i % 3 + 1;
            count = 0;
        } else {
            count++;
        }
        lock.unlock();
    }

    public static void main(String[] args) {
        TestSequential2 demo = new TestSequential2();
        for (int i = 1; i <=3; i++) {
            int j = i;
            new Thread(()->{
                while(true) {
                    demo.one(j);
                }
            }).start();
        }
    }
}

輸出io

T-1 0
T-2 0
T-3 323
T-1 54
T-2 68964
T-3 97642
T-1 6504
T-2 100603
T-3 6989
T-1 1313
T-2 0
T-3 183741
T-1 233
T-2 5081
T-3 164367
..

.class

方法四: 使用可重入鎖, 啓用公平鎖

和3同樣, 可是使用公平鎖, 這種狀況下基本上能夠作到順序執行, 偶爾會產生多一次循環循環

private final Lock lock = new ReentrantLock(true);

輸出

T-1 0
T-2 0
T-3 0
T-1 0
T-2 0
T-3 0
T-1 0
T-2 0
T-3 0
T-1 0
T-2 0
T-3 1
T-1 1
T-2 1
T-3 1
...

.

方法五: 使用Condition

每一個線程若是看到不是本身的計數, 就await(), 若是是本身的計數, 就完成打印動做, 再signalAll()全部其餘線程去繼續運行, 本身在下一個循環後, 即便又繼續執行, 也會由於計數已經變了而await.

若是ReentrantLock構造參數使用true, 能夠基本消除 ~await 這一步的輸出.

public class ReentrantLockCondition2 {
    private static Lock lock = new ReentrantLock();
    private static Condition condition = lock.newCondition();
    private volatile int state = 1;

    private void handle(int state) {
        lock.lock();
        try {
            while(true) {
                while(this.state != state) {
                    System.out.println(state + " ~await");
                    condition.await();
                }
                System.out.println(state);
                this.state = state % 3 + 1;
                condition.signalAll();
                System.out.println(state + " await");
                condition.await();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentrantLockCondition2 rlc = new ReentrantLockCondition2();
        new Thread(()->rlc.handle(1)).start();
        new Thread(()->rlc.handle(2)).start();
        new Thread(()->rlc.handle(3)).start();
    }
}

.

方法六: 使用多個Condition

給每一個線程不一樣的condition. 這個和4的區別是, 能夠用condition.signal()精確地通知對應的線程繼續執行(在對應的condition上await的線程, 多是多個). 這種狀況下是能夠多個線程都不unlock鎖的狀況下進行協做的. 注意下面的while(true)循環是在lock.lock()方法內部的.

public class ReentrantLockCondition {
    private static Lock lock = new ReentrantLock();
    private static Condition[] conditions = {lock.newCondition(), lock.newCondition(), lock.newCondition()};
    private volatile int state = 1;

    private void handle(int state) {
        lock.lock();
        try {
            while(true) {
                while(this.state != state) {
                    conditions[state - 1].await();
                }
                System.out.println(state);
                this.state = state % 3 + 1;
                conditions[this.state - 1].signal();
                conditions[state - 1].await();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ReentrantLockCondition rlc = new ReentrantLockCondition();
        new Thread(()->rlc.handle(1)).start();
        new Thread(()->rlc.handle(2)).start();
        new Thread(()->rlc.handle(3)).start();
    }
}

.

相關文章
相關標籤/搜索