Java多个线程顺序打印数字

Posted milton

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多个线程顺序打印数字相关的知识,希望对你有一定的参考价值。

要求

启动N个线程, 这N个线程要不间断按顺序打印数字1-N. 将问题简化为3个线程无限循环打印1到3

方法一: 使用synchronized

三个线程无序竞争同步锁, 如果遇上的是自己的数字, 就打印. 这种方式会浪费大量的循环

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();
        
    

输出

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();
        
    

输出

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做, 非公平锁, 三个线程竞争, 如果遇上的是自己的数字, 就打印. 这种方式也会浪费大量的循环

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();
        
    

输出

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
..

.

方法四: 使用可重入锁, 启用公平锁

和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

这种情况可以在非公平锁的环境下, 实现和4接近的效果. 每个线程在拿到lock后如果看到不是自己的计数, 就通过await把lock交出去, 如果是自己的计数, 就完成打印动作, 再通知所有其他线程去抢lock, 自己在下一个循环后, 即使又拿到lock, 也会因为计数已经变了而await把lock交出去.

public class TestSequential5 
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    private volatile int pos = 1;
    private volatile int count = 0;

    public void one(int i) 
        while (true) 
            System.out.println(i + " try lock");
            lock.lock();
            System.out.println(i + " has lock");
            try 
                while (pos != i) 
                    System.out.println(i + " await");
                    count++;
                    condition.await();
                
                System.out.println("T-" + i + " " + count);
                pos = pos % 3 + 1;
                condition.signalAll();
                count = 0;
             catch (InterruptedException e) 
                e.printStackTrace();
             finally 
                System.out.println(i + " unlock");
                lock.unlock();
            
        
    

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

输出, 可以看到具体的线程序列, 每个线程是如何得到lock, 再进入await的

2 try lock
3 try lock
2 has lock
2 await
1 try lock
1 has lock
T-1 1
1 unlock
1 try lock
1 has lock
1 await
3 has lock
3 await
T-2 2
2 unlock
2 try lock
2 has lock
2 await
1 await
T-3 2
3 unlock
3 try lock
2 await
T-1 1
1 unlock
1 try lock
1 has lock
1 await
3 has lock
3 await
T-2 2
2 unlock
2 try lock
2 has lock
2 await
1 await
T-3 2
3 unlock
3 try lock
3 has lock
3 await
2 await
T-1 2
1 unlock
1 try lock
1 has lock
1 await
3 await
T-2 2
2 unlock
2 try lock
1 await
T-3 1
3 unlock
3 try lock
2 has lock
2 await
T-1 1
1 unlock
1 try lock
3 has lock
3 await
T-2 1
2 unlock
2 try lock
2 has lock
2 await
1 has lock
1 await
T-3 2
...

.

方法六: 使用多个Condition

给每个线程不同的condition. 这个和4的区别是, 会精确地通知对应的线程(在对应的condition上await的线程, 可能是多个)去抢lock.

public class TestSequential3 
    private final Lock lock = new ReentrantLock();
    private final Condition[] conditions = lock.newCondition(), lock.newCondition(), lock.newCondition();
    private volatile int pos = 1;
    private volatile int count = 0;

    public void one(int i) 
        while (true) 
            System.out.println(i + " try lock");
            lock.lock();
            System.out.println(i + " has lock");
            try 
                while (pos != i) 
                    System.out.println(i + " await");
                    count++;
                    conditions[i - 1].await();
                
                System.out.println("T-" + i + " " + count);
                pos = pos % 3 + 1;
                conditions[pos - 1].signal();
                count = 0;
             catch (InterruptedException e) 
                e.printStackTrace();
             finally 
                System.out.println(i + " unlock");
                lock.unlock();
            
        
    

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

输出

2 try lock
1 try lock
3 try lock
2 has lock
2 await
1 has lock
T-1 1
1 unlock
1 try lock
3 has lock
3 await
T-2 1
2 unlock
2 try lock
1 has lock
1 await
T-3 1
3 unlock
3 try lock
3 has lock
3 await
2 has lock
2 await
T-1 2
1 unlock
1 try lock
T-2 0
2 unlock
2 try lock
1 has lock
1 await
T-3 1
3 unlock
3 try lock
3 has lock
3 await
2 has lock
2 await
T-1 2
1 unlock
1 try lock
T-2 0
2 unlock
2 try lock
1 has lock
1 await
T-3 1
...

.

 

以上是关于Java多个线程顺序打印数字的主要内容,如果未能解决你的问题,请参考以下文章

多个线程顺序打印问题

多线程编程——面试题,每个线程只打印一种字符,多个线程协同顺序打印n次字符串(求大神的其他实现方案)

操作系统利用信号量多个线程轮流打印数字

JAVA并发多个线程交替打印

JAVA并发多个线程交替打印

java多线程连续打印字母数字问题