多线程编程步骤

  1. 创建资源类,在资源类创建属性和操作方法
  2. 在资源类操作方法

             1判断

              2.干活

              3.通知

      3. 创建多个线程,调用资源类的操作方法

      4. 防止虚假唤醒问题   (wait()方法要放在循环里)

package lock;


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

class Shareess {
    private int number = 0;
    private Lock locks = new ReentrantLock();

    private Condition condition = locks.newCondition();

    //+1
    public void incr() {
        locks.lock();
        try {
            //判断
            while (number != 0) {
                condition.await();
            }

            //干活
            number++;
            System.out.println(Thread.currentThread().getName() + "::" + number);
            //通知
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            locks.unlock();
        }

    }
    //-1
    public void decr() throws InterruptedException {
        locks.lock();
        try {
            while (number!=1){
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName()+"::"+number);
            condition.signalAll();
        }finally {
            locks.unlock();
        }
    }

}

public class ThreadDemo2 {
    public static void main(String[] args) {
        Shareess shareess = new Shareess();
        new Thread(()->{
            for (int i =1;i<10;i++){
                shareess.incr();
            }

        },"aa").start();

        new Thread(()->{
            for (int i =1;i<10;i++){
                try {
                    shareess.decr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"bb").start();



        new Thread(()->{
            for (int i =1;i<10;i++){
                shareess.incr();
            }

        },"ccc").start();

        new Thread(()->{
            for (int i =1;i<10;i++){
                try {
                    shareess.decr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"ddd").start();
    }
}

线程间的定制化通信

package lock;

//第一步 创建资源类

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

class ShareResource{
    //定义标志位
    private int flag =1; //1 AA  2 BB  3 CC
    private Lock lock = new ReentrantLock();

    //创建三个condition
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    //打印5次,参数几轮
    public void print5(int loop) throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (flag !=1){
                //等待
                c1.await();
            }

            //干活
            for (int i =1;i<=5;i++){
                System.out.println(Thread.currentThread().getName()+"::"+i+":轮数:"+loop);
            }
            //通知
            flag=2;
            c2.signal();
        }finally {
            lock.unlock();
        }
    }
    //打印10次,参数几轮
    public void print10(int loop) throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (flag !=2){
                //等待
                c2.await();
            }

            //干活
            for (int i =1;i<=5;i++){
                System.out.println(Thread.currentThread().getName()+"::"+i+":轮数:"+loop);
            }
            //通知
            flag=3;
            c3.signal();
        }finally {
            lock.unlock();
        }
    }


    //打印15次,参数几轮
    public void print15(int loop) throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (flag !=3){
                //等待
                c3.await();
            }

            //干活
            for (int i =1;i<=5;i++){
                System.out.println(Thread.currentThread().getName()+"::"+i+":轮数:"+loop);
            }
            //通知
            flag=1;
            c1.signal();
        }finally {
            lock.unlock();
        }
    }
}
public class ThreadDemo3 {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{
            for (int i =1;i<=10;i++){
                try {
                    shareResource.print5(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"aa").start();


        new Thread(()->{
            for (int i =1;i<=10;i++){
                try {
                    shareResource.print10(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"bb").start();


        new Thread(()->{
            for (int i =1;i<=10;i++){
                try {
                    shareResource.print15(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"cc").start();
    }
}

运行结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值