java读者与写者问题,java 并发(4) 读者写者有关问题的几种实现方法及性能比较...

读者-写者问题

1、读不阻塞其他读

2、读阻塞其他写

3、写阻塞其他读与其他写

问题分为两种:读者优先与写者优先

使用java来解决该问题有很多常用的方法,包括使用synchronized,使用Semaphore信号量等。

1、使用读锁与写锁来解决,这种方法最简单直观

package com.xx.concurrent.commonUse;

import java.util.concurrent.CountDownLatch;

public class ReaderAndWriterWithMonitor {

//读锁

static Object w = new Object();

//写锁

static Object r = new Object();

//内容

static int data = 0 ;

static CountDownLatch latch = new CountDownLatch(150);

class Reader extends Thread {

int quantity;

Reader(int quantity) {

this.quantity = quantity;

}

@Override

public void run() {

synchronized (w) {

while (quantity > 0) {

System.out.println(getName()

+ " is reading ...【data=" + data + "】");

quantity--;

}

latch.countDown();

}

}

}

class Writer extends Thread {

int quantity;

Writer(int quantity) {

this.quantity = quantity;

}

@Override

public void run() {

synchronized (w) {

synchronized (r) {

while (quantity > 0) {

data++;

System.out.println(getName() + " is writing...【data=" + data + "】");

quantity--;

}

latch.countDown();

}

}

}

}

public static void main(String[] args) throws InterruptedException {

long startTime = System.nanoTime();

ReaderAndWriterWithMonitor demo = new ReaderAndWriterWithMonitor();

for (int i = 0; i < 100; ++i) {

demo.new Reader(10).start();

}

for (int i = 0; i < 50; ++i) {

demo.new Writer(1).start();

}

latch.await();

long endTime = System.nanoTime();

System.out.println(endTime - startTime + "ns");

}

}

2、使用信号量

package com.xx.concurrent.commonUse;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

public class ReaderAndWriter {

static Semaphore mutex = new Semaphore(1);

static Semaphore w = new Semaphore(1);

static int readcnt = 0 ;

static CountDownLatch latch = new CountDownLatch(150);

static int data = 0;

class Reader extends Thread{

int quantity;

Reader(int quantity){

this.quantity = quantity;

}

@Override

public void run(){

while(quantity > 0){

try {

mutex.acquire();

readcnt++;

if (readcnt == 1)

w.acquire();

mutex.release();

//read something

System.out.println(getName() + " is reading ...【data=" + data + "】");

mutex.acquire();

readcnt--;

if (readcnt == 0)

w.release();

mutex.release();

quantity--;

} catch (InterruptedException e) {

e.printStackTrace();

}

}

latch.countDown();

}

}

class Writer extends Thread{

int quantity;

Writer(int quantity){

this.quantity = quantity;

}

@Override

public void run(){

while(quantity>0){

try {

w.acquire();

data++;

System.out.println(getName() + " is writing ...【data=" + data + "】");

w.release();

quantity--;

} catch (InterruptedException e) {

e.printStackTrace();

}

}

latch.countDown();

}

}

public static void main(String[] args) throws InterruptedException {

long startTime = System.nanoTime();

ReaderAndWriter demo = new ReaderAndWriter();

ExecutorService service = Executors.newFixedThreadPool(150);

for(int i=0; i< 100; ++i){

service.execute(demo.new Reader(10));

}

for(int i=0 ; i< 50; ++i){

service.execute(demo.new Writer(1));

}

latch.await();

service.shutdown();

long endTime = System.nanoTime();

System.out.println(endTime - startTime + "ns");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值