synchronized 读写同步
synchronized 读写同步 这是一道面试题,很多人也遇到了。 要求:1.读-读 不用线程同步。2.读-写 要求线程同步,写的时候不能读。3.写-写同步。写的时候不能写。 java lock读写锁是好的处理方案。这里不说了。但人家问的是synchronized 读写同步。 假设两个方法,write() ,read();
- read()之间不需要同步,正常的就行。
- read()与write()之间需要同步。
- write()之间需要同步。
1.和3.很好理解也很现实。 经过思考:write()必须是同步的,所有对象都要同步。那这里需要设计成静态的。如果不静态的,保证每次同步都是一个锁。
其次 read() 之前要栓查write同步锁,但又没有对应方法可以检查。想到的是 再写一个栓查方法。这个方法与write使用同一个同步锁(设计在同一个类,同时静态)。但这个方法是空的,什么也不做。
所以设计成如下代码:
package test;
public class Test {
public void read() {
canRead();
System.out.println(Thread.currentThread().getName() + "-->>begain read");
System.out.println(Thread.currentThread().getName() + "-->>reading");
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "-->>read end");
}
public static synchronized void canRead() {
}
public static synchronized void write() {
System.out.println(Thread.currentThread().getName() + "-->>begain write");
canRead();
System.out.println(Thread.currentThread().getName() + "-->>writing");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "-->>writing end");
return;
}
public static void main(String[] args) {
Test test = new Test();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
test.read();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.setName("read_thread_1");
t.start();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
test.read();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.setName("read_thread_2");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Test.write();
}
}
});
t2.setName("write_thread");
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Test.write();
}
}
});
t3.setName("write_thread_2");
t3.start();
}
}
运行结果如下,通过结果分析,读线程是穿插的,不存在同步。写线程都是完整执行过程,是同步的。
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>begain read
read_thread_2-->>reading
read_thread_1-->>read end
read_thread_2-->>read end
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>begain read
read_thread_2-->>reading
read_thread_1-->>read end
read_thread_2-->>read end
write_thread-->>begain write
write_thread-->>writing
write_thread-->>writing end
read_thread_2-->>begain read
read_thread_2-->>reading
read_thread_1-->>begain read
read_thread_1-->>reading
write_thread_2-->>begain write
write_thread_2-->>writing
write_thread_2-->>writing end
read_thread_2-->>read end
read_thread_1-->>read end
read_thread_2-->>begain read
read_thread_2-->>reading
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>read end
read_thread_1-->>read end
write_thread-->>begain write
write_thread-->>writing
write_thread-->>writing end
write_thread_2-->>begain write
write_thread_2-->>writing
write_thread_2-->>writing end
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>begain read
read_thread_2-->>reading
read_thread_1-->>read end
read_thread_2-->>read end
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>begain read
read_thread_2-->>reading
write_thread-->>begain write
write_thread-->>writing
read_thread_1-->>read end
read_thread_2-->>read end
write_thread-->>writing end
write_thread_2-->>begain write
write_thread_2-->>writing
write_thread_2-->>writing end
read_thread_2-->>begain read
read_thread_2-->>reading
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>read end
read_thread_1-->>read end
read_thread_2-->>begain read
read_thread_1-->>begain read
read_thread_1-->>reading
read_thread_2-->>reading
write_thread-->>begain write
write_thread-->>writing
这个代码冗余部分是要执行一个空方法,目前还没有想到其它方法,有大牛有好办法可以留言互相学习。
代码 bug :开始写的时候还没有读完的线程。会导致错误。
-----待改进-----------
synchronized 读写同步的更多相关文章
- Java多线程初学者指南(12):使用Synchronized块同步变量
我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特性的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...
- Java多线程初学者指南(10):使用Synchronized关键字同步类方法
要想解决“脏数据”的问题,最简单的方法就是使用synchronized关键字来使run方法同步,代码如下: public synchronized void run() { ... } 从上面的代码可 ...
- 浅谈linux读写同步机制RCU
RCU是linux系统的一种读写同步机制,说到底他也是一种内核同步的手段,本问就RCU概率和实现机制,给出笔者的理解. [RCU概率] 我们先看下内核文档中对RCU的定义: RCU is a sync ...
- java synchronized 线程同步机制详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...
- 深入理解synchronized方法同步的是方法还是对象?
一.运用synchronized关键字 首先我们来看看一个多线程中线程不安全的列子 代码如下: 共享数据类: public class NotSynchronizated extends Thread ...
- Java的synchronized的同步代码块和同步方法的区别
synchronized同步方法和同步代码块的区别 同步方法默认使用this或者当前类做为锁. 同步代码块可以选择以什么来加锁,比同步方法更精确,我们可以选择只有会在同步发生同步问题的代码加锁,而并不 ...
- 使用Synchronized块同步变量
我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特定的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...
- 使用Synchronized关键字同步类方法
要想解决“脏数据”的问题,最简单的方法就是使用synchronized关键字来使run方法同步,代码如下: public synchronized void run() { } 从上面的代码可以看出, ...
- Java多线程简析——Synchronized(同步锁)、Lock以及线程池
Java多线程 Java中,可运行的程序都是有一个或多个进程组成.进程则是由多个线程组成的.最简单的一个进程,会包括mian线程以及GC线程. 线程的状态 线程状态由以下一张网上图片来说明: 在图中, ...
随机推荐
- 06 基本数据结构 - 双端队列(Deque)
一.双端队列(Deque) - 概念:deque(也称为双端队列)是与队列类似的项的有序集合.它有两个端部,首部和尾部,并且项在集合中保持不变. - 特性:deque 特殊之处在于添加和删除项是非限制 ...
- Java 获取日期间的日期 & 根据日期获取星期
场景:根据起止日期获取中间的日期: 根据日期获取当前日期的星期 根据日期日期获取日期 /** * 获取日期间日期 * @param start * @param end * @return */ pr ...
- python 3.8 新特性
董伟明技术博客 安装 python 3.8 环境 , 在此刻 似乎 anaconda 都还不支持 3.8 ,所以直接下载源码进行编译安装 环境: centos7.5 版本:python3.8 1.依赖 ...
- Linux系统文件系统及文件基础篇
学习Linux,重难点在于掌握不同类别的文件系统及其作用.通过对Linux系统的安装,我们首先来了解下Linux系统里各个目录文件夹下的大致功能:主要的目录树的有/./root./home./usr. ...
- Go语言标准库之fmt.Scan
Go语言fmt.Scan使用指南 本文介绍了Go语言中fmt包中从标准输入获取数据的的Scan系列函数.从io.Reader中获取数据的Fscan系列函数以及从字符串中获取数据的Sscan系列函数的用 ...
- composer入门教程
初始化项目 使用composer初始化工作目录,在项目的根目录命令行输入 composer init 安装项目 在composer.json文件所在目录命令行下执行如下命令 php composer. ...
- 01--springmvc分布式项目Web项目配置
springmvc的配置文件,放在resources目录下: 文件名:applicationContext-mvc.xml <?xml version="1.0" encod ...
- AIX 下的 find 命令使用
平常我们使用 find , -size +100M/K/G ,就可以找到相应大小的文件了. 可是 AIX 平台下,却好像不能使用,虽然执行起来不报错,但是查找出来的文件却并不是我们想要的.所以 m ...
- java8学习之Collector源码分析与收集器核心
之前已经对流在使用上已经进行了大量应用了,也就是说对于它的应用是比较熟悉了,但是比较欠缺的是对于它底层的实现还不太了解,所以接下来准备大量通过阅读官方的javadoc反过来加深对咱们已经掌握这些知识更 ...
- solr创建core
创建Core的两种方法: 第一种方法: 1.打开dos命令窗口,切换目录到${solr.home}\bin,然后输入:solr create -c corename之后回车: 2.打开solr安装文件 ...