线程同步:顺序打印的问题。以及synchronized与Lock

关于synchronized与Lock可参考:

详解synchronized与Lock的区别与使用

Lock和synchronized的区别和使用


以下是关于多线程顺序打印问题。

一、有A、B、C四个线程,A线程输出A, B线程输出B, C线程输出C,要求, 同时启动3个线程, 按顺序输出AABBCC;

思路:A执行完,执行B,再执行C。考虑使用Thread.join()方法,保证此执行顺序。代码如下:

    /**
     * 顺序打印:aabbcc
     */
    private void threadTest() {
        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < anInt; i++) {
                    Log.i(TAG, "run: a" + i);
                    if (anInt == 2)
                    {
                        Thread.yield();
                    }
                }
            }
        });

        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    thread1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < anInt; i++) {
                    Log.i(TAG, "run: b"+ i);
                }
            }
        });

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    thread2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < anInt; i++) {
                    Log.i(TAG, "run: c"+ i);
                }
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }

结果如下:

07-14 22:14:37.406 28606-28747/com.hfy.demo00 I/MainActivityhfy: run: a0
    run: a1
07-14 22:14:37.407 28606-28748/com.hfy.demo00 I/MainActivityhfy: run: b0
    run: b1
07-14 22:14:37.408 28606-28749/com.hfy.demo00 I/MainActivityhfy: run: c0
    run: c1


二、有A、B、C四个线程,A线程输出A, B线程输出B, C线程输出C,要求, 同时启动3个线程,循环5次 顺序打印:abcabcabcabcabc

思路:每个循环打印10次,但是如何保证每个线程执行完第一次后让其他线程执行?考虑使用Lock,因为锁的释放,可以控制。即执行完第一次后可以释放锁。synchronized只能全部执行完才会释放锁。

    /**
     * 循环10次 顺序打印:abc abc abc ...
     */
    private void threadTest2() {
        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {

                    for (int i = 0; i < anInt; ) {
                        lock.lock();
                        try{
                            if (state % 3 == 0)
                            {
                                Log.i(TAG, "run: a" + i);
                                i++;
                                state++;
                            }
                        }
                        finally {
                            //每次遍历,都释放锁。以便其他线程获取锁。获取锁后,该线程是否打印,由state决定。
                            lock.unlock();
                        }
                    }
            }
        });

        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < anInt; ) {
                    lock.lock();
                    try{
                        if (state % 3 == 1)
                        {
                            Log.i(TAG, "run: b" + i);
                            i++;
                            state++;
                        }
                    }
                    finally {
                        //每次遍历,都释放锁。以便其他线程获取锁。获取锁后,该线程是否打印,由state决定。
                        lock.unlock();
                    }
                }
            }
        });

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < anInt; ) {
                    lock.lock();
                    try{
                        if (state % 3 == 2)
                        {
                            Log.i(TAG, "run: c" + i);
                            i++;
                            state++;
                        }
                    }
                    finally {
                        //每次遍历,都释放锁。以便其他线程获取锁。获取锁后,该线程是否打印,由state决定。
                        lock.unlock();
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }

结果如下:

07-14 22:21:43.205 29221-29463/com.hfy.demo00 I/MainActivityhfy: run: a0
07-14 22:21:43.206 29221-29464/com.hfy.demo00 I/MainActivityhfy: run: b0
07-14 22:21:43.207 29221-29465/com.hfy.demo00 I/MainActivityhfy: run: c0
07-14 22:21:43.208 29221-29463/com.hfy.demo00 I/MainActivityhfy: run: a1
07-14 22:21:43.210 29221-29464/com.hfy.demo00 I/MainActivityhfy: run: b1
07-14 22:21:43.211 29221-29465/com.hfy.demo00 I/MainActivityhfy: run: c1
07-14 22:21:43.212 29221-29463/com.hfy.demo00 I/MainActivityhfy: run: a2
07-14 22:21:43.214 29221-29464/com.hfy.demo00 I/MainActivityhfy: run: b2
07-14 22:21:43.215 29221-29465/com.hfy.demo00 I/MainActivityhfy: run: c2
07-14 22:21:43.216 29221-29463/com.hfy.demo00 I/MainActivityhfy: run: a3
07-14 22:21:43.217 29221-29464/com.hfy.demo00 I/MainActivityhfy: run: b3
07-14 22:21:43.219 29221-29465/com.hfy.demo00 I/MainActivityhfy: run: c3
07-14 22:21:43.219 29221-29463/com.hfy.demo00 I/MainActivityhfy: run: a4
07-14 22:21:43.220 29221-29464/com.hfy.demo00 I/MainActivityhfy: run: b4
07-14 22:21:43.220 29221-29465/com.hfy.demo00 I/MainActivityhfy: run: c4


你可能感兴趣的:(java)