栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

信号量java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

信号量java

java 信号量实现 基本 读写锁

1.通过实现信号量 信号量控制读写过程

信号量可以实现互斥操作以及同步操作,在多线程中控制多个cpu对临界资源的互斥访问。
这里实现的是读写锁中读优先的代码,因为有读者的线程在读 都是把写者线程给阻塞了的,直到读者都没了才允许写操作。
代码如下

package mutex;

public class Signal {
    //本身存在的互斥锁
    private final Integer signal_mutex = 0;
    private int count;//表示当前锁资源为1

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Signal(int count) {
        this.count = count;
    }

    //获取锁 必须为原子操作
    public void p() {
        synchronized (signal_mutex) {
            count--;
            if (count < 0) {
                //当前线程等待
                try {
                    signal_mutex.wait();
                } catch (Exception e) {

                }
            }
        }
    }

    //释放锁 必须为原子操作
    public void v() {
        synchronized (signal_mutex) {
            count++;
            if (count <= 0) {
                signal_mutex.notify();//唤醒一个
            }
        }
    }
}
 

读写操作

package mutex;

import java.util.ArrayList;
import java.util.List;


public class ReaderWriter {
    public static int readCount = 0;
    //写者锁
    private static Signal write_metux = new Signal(1);
    //读者锁 锁住readCount
    private static Signal read_metux = new Signal(1);

    //write
    public static void write() {
        //表示同一个线程能多次去写
        write_metux.p();
        //开始写数据
        System.out.println("开始写数据");
        try {
            System.out.println(Thread.currentThread().getName() + "等待5秒");
            Thread.sleep(5000);
        } catch (Exception e) {

        }
        write_metux.v();
    }

    //read
    public static void read() {
        read_metux.p();//控制读者的数量 不为0都会阻塞写线程
        if (readCount == 0) {
            System.out.println("写线程被阻塞");
            write_metux.p();//阻塞写操作
        }
        readCount++;
        read_metux.v();
        System.out.println("开始读数据");
        read_metux.p();
        readCount--;
        if (readCount == 0) {
            write_metux.v();//释放写锁 让写操作能继续
        }
        read_metux.v();
    }

    public static void main(String[] args) {
        List threadList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    read();
                }
            });
            thread.setName("线程" + i);
            threadList.add(thread);
        }
        Thread writeThread = new Thread(new Runnable() {
            @Override
            public void run() {
                write();
            }
        });
        writeThread.setName("写线程");
        threadList.add(writeThread);
        for (int i = 0; i < threadList.size(); i++) {
            threadList.get(i).start();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/914243.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号