转---秒杀多线程第十二篇 多线程同步内功心法——PV操作上 (续)
PV操作的核心就是 PV操作可以同时起到同步与互斥的作用。
1.同步就是通过P操作获取信号量,V操作释放信号量来进行。
2.互斥其实就是,同时操作P操作,结束后进行V操作即可做到。
Java上实现PV操作可以通过Semaphore来实现。
package com.multithread.pvoperator; import java.util.concurrent.Semaphore; /*
P(S): ①将信号量S的值减1,即S=S-1; ②如果S>=0,则该进程继续执行;否则该进程置为等待状态。 V(S): ①将信号量S的值加1,即S=S+1; ②该进程继续执行;如果该信号的等待队列中有等待进程就唤醒一等待进程。
*
* */
public class PVObject { private Semaphore mSemaphore =null;
private int Max_size = 0xff;
private String name = null;
public PVObject(int size,String name)
{
if(size>0)
{
Max_size = size;
mSemaphore = new Semaphore(size);
}
this.name = name;
} public PVObject(String name)
{
Max_size = 1;
mSemaphore = new Semaphore(1);
this.name = name;
} public void Init(int status)
{
if(status<0 || status>Max_size)
{
System.out.println("[PVObject][Init]"+name+" wrong,status:"+status);
return;
} if(status == Max_size)
{
return;
} try {
mSemaphore.release(Max_size);
mSemaphore.acquire(Max_size-status);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void P()
{
try {
//
mSemaphore.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void V()
{
mSemaphore.release();
}
}
分水果问题Java是实现:
package com.multithread.pvoperator; import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors; public class Fruit {
/*
* 下面先考虑同步情况即所有“等待”情况:
第一.爸爸要等待盘子为空。
第二.儿子要等待盘中水果是桔子。
第三.女儿要等待盘中水果是苹果。
接下来来考虑要互斥处理的资源,看起来盘子好像是要作互斥处理的,
但由于题目中的爸爸、儿子、女儿均只有一个,并且他们访问盘子的条件都不一样,
所以他们根本不会同时去访问盘子,因此盘子也就不用作互斥处理了
*
* */
public PVObject mEmptyDash = new PVObject("emptyDash");//
public PVObject mApple = new PVObject("apple"); //
public PVObject mOranger = new PVObject("oranger"); //
public boolean mDadEnd = false;
public CountDownLatch mLatchDown = new CountDownLatch(3);
public CountDownLatch mLatchStart = new CountDownLatch(3);
public Queue<Integer> mQueue = new LinkedList<Integer>();
public void Start()
{
mEmptyDash.Init(1);
mApple.Init(0);
mOranger.Init(0);
mQueue.clear();
Executor mEcecutor = Executors.newFixedThreadPool(5);
mEcecutor.execute(new Dad(this));
mEcecutor.execute(new Daughter(this));
mEcecutor.execute(new Son(this)); try {
mLatchStart.await();
System.out.println("all thread start"); mLatchDown.await();
System.out.println("all thread down");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public class Dad extends Thread{ public Fruit mFruit = null;
boolean flag = true;
public int MAX_FRUIT_COUNT = 20;
public int index = 0;
public Dad(Fruit f)
{
mFruit = f;
}
@Override
public void run() {
mLatchStart.countDown();
while(flag)
{
mFruit.mEmptyDash.P(); index++;
if(index >=MAX_FRUIT_COUNT)
{
flag = false;
} mQueue.offer(index); if((int)(Math.random()*2) == 1)
{
System.out.println("dad put apple"+index+" to dash");
//apply
mFruit.mApple.V();
}
else
{
//oranger
System.out.println("dad put oranger"+index+" to dash");
mFruit.mOranger.V();
}
}
mFruit.mDadEnd = true;
System.out.println("dad thread is end");
mLatchDown.countDown();
}
} public class Daughter extends Thread{ public Fruit mFruit = null;
boolean flag = true;
public Daughter(Fruit f)
{
mFruit = f;
}
@Override
public void run() {
mLatchStart.countDown();
while(flag)
{
mFruit.mOranger.P();
if(mQueue.size()>0)
{
System.out.println("Daughter get oranger"+mQueue.poll()+" from dash");
mFruit.mEmptyDash.V();
}
else
{
System.out.println("Daughter get oranger from dash,but dash is empty");
} if(mFruit.mDadEnd == true)
{
flag = false;
}
}
System.out.println("Daughter thread is end");
//notify son down,for this dad is down.
mApple.V();
mLatchDown.countDown();
}
} public class Son extends Thread{ public Fruit mFruit = null;
boolean flag = true;
public Son(Fruit f)
{
mFruit = f;
}
@Override
public void run() {
mLatchStart.countDown();
while(flag)
{
mFruit.mApple.P();
if(mQueue.size()>0)
{
System.out.println("Son get apple"+mQueue.poll()+" from dash");
mFruit.mEmptyDash.V();
}
else
{
System.out.println("Son get apple from dash,but dash is empty");
} if(mFruit.mDadEnd == true)
{
flag = false;
}
}
System.out.println("Son thread is end");
mOranger.V();
mLatchDown.countDown();
}
}
}
安全岛问题:
package com.multithread.pvoperator; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors; public class SafeIsland { public PVObject NT = new PVObject("NLoad");
public PVObject TN = new PVObject("TLoad");
public PVObject K = new PVObject("K");
public PVObject L = new PVObject("L");
public static final int MAX_NANKAI_CAR_COUNT = 2;
public static final int MAX_TIANJING_CAR_COUNT = 3;
public CountDownLatch mLatchDown = new CountDownLatch(MAX_NANKAI_CAR_COUNT+MAX_TIANJING_CAR_COUNT); public class NanKaiCar extends Thread{
String name = null;
public NanKaiCar(String name)
{
this.name = name;
}
@Override
public void run() {
System.out.println("[NanKaiCar]"+name+" Thread start");
try {
Thread.sleep((long) (Math.random()*100));
NT.P();
System.out.println("[NanKaiCar]"+name+" enter crossing N");
K.P();
System.out.println("[NanKaiCar]"+name+" walk to M:N->M");
Thread.sleep((long) (Math.random()*1000));
System.out.println("[NanKaiCar]"+name+" start walk to T");
K.V();
L.P();
System.out.println("[NanKaiCar]"+name+" walk to T:M->T");
L.V();
NT.V();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mLatchDown.countDown();
System.out.println("[NanKaiCar]"+name+" walk down");
} } public class TianJingCar extends Thread{
String name = null;
public TianJingCar(String name)
{
this.name = name;
}
@Override
public void run() { try {
System.out.println("[TianJingCar]"+name+" Thread start");
Thread.sleep((long) (Math.random()*100));
TN.P();
System.out.println("[TianJingCar]"+name+" enter crossing T");
L.P();
System.out.println("[TianJingCar]"+name+" walk to M:T->M");
Thread.sleep((long) (Math.random()*1000));
System.out.println("[TianJingCar]"+name+" start walk to N");
L.V();
K.P();
System.out.println("[TianJingCar]"+name+" walk to T:M->N");
K.V();
TN.V();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mLatchDown.countDown();
System.out.println("[TianJingCar]"+name+" walk down");
} } public void start()
{
NT.Init(1);
TN.Init(1);
K.Init(1);
L.Init(1);
Executor mEcecutor = Executors.newFixedThreadPool(MAX_TIANJING_CAR_COUNT+MAX_NANKAI_CAR_COUNT+1);
for(int i =1;i<=MAX_NANKAI_CAR_COUNT;i++)
{
mEcecutor.execute(new NanKaiCar("carN"+i));
}
for(int j=1;j<=MAX_TIANJING_CAR_COUNT;j++)
{
mEcecutor.execute(new TianJingCar("carT"+j));
}
try {
mLatchDown.await();
System.out.println("all car has pass road");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
生产消费者问题伪代码:
package com.multithread.pvoperator;
public class Prosumer {
//PV 分析 生产者,消费者问题
/*同步: 生产者:缓冲区有空间,就放入数据 P(EmptyS) 只有空和不空,信号量为1
* 消费者:缓冲区有数据,就读取数据,并移走数据 P(NotEmptyS),信号量为缓冲区大小
*互斥: 生产者 写入数据,和消费者移走数据互斥 P(OperatorS),用来互斥,信号量为1
* 消费者异步读取移动数据,互斥
* */
public class Productor extends Thread{
@Override
public void run() {
while(true)
{
P(EmptyS);
P(OperatorS);
//operator data
V(OperatorS);
V(NotEmptyS);//通知不为空
}
}
}
public class Consumer extends Thread{
@Override
public void run() {
P(NotEmptyS);
P(OperatorS);
//operator data
V(OperatorS);
V((EmptyS);
}
}
}
package com.multithread.pvoperator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class Fruit {
/*
* 下面先考虑同步情况即所有“等待”情况:
第一.爸爸要等待盘子为空。
第二.儿子要等待盘中水果是桔子。
第三.女儿要等待盘中水果是苹果。
接下来来考虑要互斥处理的资源,看起来盘子好像是要作互斥处理的,
但由于题目中的爸爸、儿子、女儿均只有一个,并且他们访问盘子的条件都不一样,
所以他们根本不会同时去访问盘子,因此盘子也就不用作互斥处理了
*
* */
public PVObject mEmptyDash = new PVObject("emptyDash");//1
public PVObject mApple = new PVObject("apple"); //0
public PVObject mOranger = new PVObject("oranger"); //0
public boolean mDadEnd = false;
public CountDownLatch mLatchDown = new CountDownLatch(3);
public CountDownLatch mLatchStart = new CountDownLatch(3);
public Queue<Integer> mQueue = new LinkedList<Integer>();
public void Start()
{
mEmptyDash.Init(1);
mApple.Init(0);
mOranger.Init(0);
mQueue.clear();
Executor mEcecutor = Executors.newFixedThreadPool(5);
mEcecutor.execute(new Dad(this));
mEcecutor.execute(new Daughter(this));
mEcecutor.execute(new Son(this));
try {
mLatchStart.await();
System.out.println("all thread start");
mLatchDown.await();
System.out.println("all thread down");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Dad extends Thread{
public Fruit mFruit = null;
boolean flag = true;
public int MAX_FRUIT_COUNT = 20;
public int index = 0;
public Dad(Fruit f)
{
mFruit = f;
}
@Override
public void run() {
mLatchStart.countDown();
while(flag)
{
mFruit.mEmptyDash.P();
index++;
if(index >=MAX_FRUIT_COUNT)
{
flag = false;
}
mQueue.offer(index);
if((int)(Math.random()*2) == 1)
{
System.out.println("dad put apple"+index+" to dash");
//apply
mFruit.mApple.V();
}
else
{
//oranger
System.out.println("dad put oranger"+index+" to dash");
mFruit.mOranger.V();
}
}
mFruit.mDadEnd = true;
System.out.println("dad thread is end");
mLatchDown.countDown();
}
}
public class Daughter extends Thread{
public Fruit mFruit = null;
boolean flag = true;
public Daughter(Fruit f)
{
mFruit = f;
}
@Override
public void run() {
mLatchStart.countDown();
while(flag)
{
mFruit.mOranger.P();
if(mQueue.size()>0)
{
System.out.println("Daughter get oranger"+mQueue.poll()+" from dash");
mFruit.mEmptyDash.V();
}
else
{
System.out.println("Daughter get oranger from dash,but dash is empty");
}
if(mFruit.mDadEnd == true)
{
flag = false;
}
}
System.out.println("Daughter thread is end");
//notify son down,for this dad is down.
mApple.V();
mLatchDown.countDown();
}
}
public class Son extends Thread{
public Fruit mFruit = null;
boolean flag = true;
public Son(Fruit f)
{
mFruit = f;
}
@Override
public void run() {
mLatchStart.countDown();
while(flag)
{
mFruit.mApple.P();
if(mQueue.size()>0)
{
System.out.println("Son get apple"+mQueue.poll()+" from dash");
mFruit.mEmptyDash.V();
}
else
{
System.out.println("Son get apple from dash,but dash is empty");
}
if(mFruit.mDadEnd == true)
{
flag = false;
}
}
System.out.println("Son thread is end");
mOranger.V();
mLatchDown.countDown();
}
}
}
转---秒杀多线程第十二篇 多线程同步内功心法——PV操作上 (续)的更多相关文章
- 多线程同步内功心法——PV操作上(未完待续。。。)
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...
- 多线程面试题系列(12):多线程同步内功心法——PV操作上
上面的文章讲解了在Windows系统下实现多线程同步互斥的方法,为了提高在实际问题中分析和思考多个线程之间同步互斥问题的能力,接下来将讲解PV操作,这也是操作系统中的重点和难点.本文将会先简要介绍下P ...
- C++第五十二篇 -- 多线程之消息传递
主线程向子线程发送消息 参考链接:https://www.cnblogs.com/ranjiewen/p/5729539.html 1. 创建线程语句 HANDLE hThread; DWORD dw ...
- “全栈2019”Java多线程第二十二章:饥饿线程(Starvation)详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第十二章:后台线程setDaemon()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第十六章:同步synchronized关键字详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- 解剖SQLSERVER 第十二篇 OrcaMDF 行压缩支持(译)
解剖SQLSERVER 第十二篇 OrcaMDF 行压缩支持(译) http://improve.dk/orcamdf-row-compression-support/ 在这两个月的断断续续的开发 ...
- 第十二篇 SQL Server代理多服务器管理
本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...
- 第十二篇 Integration Services:高级日志记录
本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...
随机推荐
- 【BZOJ4566】[HAOI2016]找相同字符
[BZOJ4566][HAOI2016]找相同字符 题面 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. 其中\(1\le ...
- Mac下 Windows 7 虚拟机成功搭建SVN服务器后如何与Xcode建立联系,并上传原始工程的详细步骤
内容中包含 base64string 图片造成字符过多,拒绝显示
- swift实现UItableview上拉下拉刷新模块
最近用写个项目 发现上拉下拉刷新模块没找到合适的 so 自己写了一个 由于最近忙 教程就不写了 里面有 直接贴地址https://github.com/DaChengTechnology/DCRefr ...
- html面试题总结
1.请描述一个网页从开始请求到最终显示的完整过程? 1).在浏览器中输入网址: 2).发送至DNS服务器并获得域名对应的WEB服务器的IP地址: 3).与WEB服务器简历TCP连接: 4).浏览器向W ...
- NO.07--我跟“ 币乎 ”的那些事
文章开头给大家安利一款app吧,就是我标题提到的,‘币乎’,一个近似于虚拟货币的论坛吧,大家可以下载试试,发文章点赞赚钱,... 好了,开始说一说今天的正题吧: 这些事情说起来其实挺惭愧的,但也不是什 ...
- v-on 事件修饰符
事件修饰符: .stop 阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当该事件在该元素本身时(不是子元素)触发时才回调 .once ...
- 完美的【去重留一】SQL
DELETE consum_record FROM consum_record, ( SELECT min(id) id, user_id, monetary, consume_time FROM c ...
- python—退出ipython3的help()
退出ipython3的help() 组合键:ctrl+z
- 数据库mysql的常规操作
1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...
- 利用Tensorflow进行自然语言处理(NLP)系列之二高级Word2Vec
本篇也同步笔者另一博客上(https://blog.csdn.net/qq_37608890/article/details/81530542) 一.概述 在上一篇中,我们介绍了Word2Vec即词向 ...