Java如何实现线程之间的互斥

 给出一个题目,有3个线程,我希望线程1向控制台输出3行信息后,线程2向控制台输出6行信息,接下来是线程3输出10行信息到控制台,如此循环10次。在一个线程输出信息的时候,其他线程不能产生干扰。

Java如何实现线程之间的互斥
package com.concurrent.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*==================================
 * 描述:有3个线程,线程1连续输出3次,然后让线程2连续输出6次,接下来线程3连续输出10次。依次循环10次
 * 
 ==================================*/
public class ConditionExample {

    public static void main(String[] args) {
        final Outer outer = new Outer();
        final int loopNum = 10;
        // 线程1
        new Thread(new Runnable() {

            @Override
            public void run() {
                for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;<&nbsp;loopNum;&nbsp;i++)&nbsp;{
                    outer.out1(i);
                }
            }
        }).start();

        //&nbsp;线程2
        new&nbsp;Thread(new&nbsp;Runnable()&nbsp;{

            @Override
            public&nbsp;void&nbsp;run()&nbsp;{
                for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;<&nbsp;loopNum;&nbsp;i++)&nbsp;{
                    outer.out2(i);
                }
            }
        }).start();

        //&nbsp;线程3
        new&nbsp;Thread(new&nbsp;Runnable()&nbsp;{

            @Override
            public&nbsp;void&nbsp;run()&nbsp;{
                for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;<&nbsp;loopNum;&nbsp;i++)&nbsp;{
                    outer.out3(i);
                }
            }
        }).start();

    }

}

class&nbsp;Outer&nbsp;{

    Lock&nbsp;lock&nbsp;=&nbsp;new&nbsp;ReentrantLock();
    Condition&nbsp;condition1&nbsp;=&nbsp;lock.newCondition();
    Condition&nbsp;condition2&nbsp;=&nbsp;lock.newCondition();
    Condition&nbsp;condition3&nbsp;=&nbsp;lock.newCondition();

    int&nbsp;currentNo&nbsp;=&nbsp;1;

    //&nbsp;线程1的输出方法
    public&nbsp;void&nbsp;out1(int&nbsp;loopNum)&nbsp;{
        try&nbsp;{
            lock.lock();
            while&nbsp;(currentNo&nbsp;!=&nbsp;1)&nbsp;{
                try&nbsp;{
                    condition1.await();
                }&nbsp;catch&nbsp;(InterruptedException&nbsp;e)&nbsp;{
                    e.printStackTrace();
                }
            }
            for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;3;&nbsp;i++)&nbsp;{
                System.out.println("this&nbsp;Thread1&nbsp;outer&nbsp;no."&nbsp;+&nbsp;i&nbsp;+&nbsp;"&nbsp;;&nbsp;loop="
                        +&nbsp;loopNum);
            }
            currentNo&nbsp;=&nbsp;2;//标志态置换为线程2可以输出的标志
            condition2.signal();//&nbsp;通知线程2开始了
        }&nbsp;finally&nbsp;{
            lock.unlock();
        }
    }

    //线程2的输出方法
    public&nbsp;void&nbsp;out2(int&nbsp;loopNum)&nbsp;{
        try&nbsp;{
            lock.lock();
            while&nbsp;(currentNo&nbsp;!=&nbsp;2)&nbsp;{
                try&nbsp;{
                    condition2.await();
                }&nbsp;catch&nbsp;(InterruptedException&nbsp;e)&nbsp;{
                    e.printStackTrace();
                }
            }
            for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;6;&nbsp;i++)&nbsp;{
                System.out.println("this&nbsp;Thread2&nbsp;outer&nbsp;no."&nbsp;+&nbsp;i&nbsp;+&nbsp;"&nbsp;;&nbsp;loop="
                        +&nbsp;loopNum);
            }
            currentNo&nbsp;=&nbsp;3;//标志态置换为线程3可以输出的标志
            condition3.signal();//通知线程3
        }&nbsp;finally&nbsp;{
            lock.unlock();
        }
    }

    //线程3的输出方法
    public&nbsp;void&nbsp;out3(int&nbsp;loopNum)&nbsp;{
        try&nbsp;{
            lock.lock();
            while&nbsp;(currentNo&nbsp;!=&nbsp;3)&nbsp;{
                try&nbsp;{
                    condition3.await();
                }&nbsp;catch&nbsp;(InterruptedException&nbsp;e)&nbsp;{
                    e.printStackTrace();
                }
            }
            for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;10;&nbsp;i++)&nbsp;{
                System.out.println("this&nbsp;Thread3&nbsp;outer&nbsp;no."&nbsp;+&nbsp;i&nbsp;+&nbsp;"&nbsp;;&nbsp;loop="
                        +&nbsp;loopNum);
            }
            currentNo&nbsp;=&nbsp;1;//标志态置换为线程2可以输出的标志
            condition1.signal();//通知线程1
        }&nbsp;finally&nbsp;{
            lock.unlock();
        }
    }
}
Java如何实现线程之间的互斥

 

Java如何实现线程之间的互斥,布布扣,bubuko.com

Java如何实现线程之间的互斥

上一篇:java String 空指针异常


下一篇:Java之多线程