synchronized 读写同步

这是一道面试题,很多人也遇到了。

要求:1.读-读 不用线程同步。2.读-写 要求线程同步,写的时候不能读。3.写-写同步。写的时候不能写。

java lock读写锁是好的处理方案。这里不说了。但人家问的是synchronized 读写同步。

假设两个方法,write() ,read();
  1. read()之间不需要同步,正常的就行。
  2. read()与write()之间需要同步。
  3. 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 读写同步的更多相关文章

  1. Java多线程初学者指南(12):使用Synchronized块同步变量

    我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特性的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...

  2. Java多线程初学者指南(10):使用Synchronized关键字同步类方法

    要想解决“脏数据”的问题,最简单的方法就是使用synchronized关键字来使run方法同步,代码如下: public synchronized void run() { ... } 从上面的代码可 ...

  3. 浅谈linux读写同步机制RCU

    RCU是linux系统的一种读写同步机制,说到底他也是一种内核同步的手段,本问就RCU概率和实现机制,给出笔者的理解. [RCU概率] 我们先看下内核文档中对RCU的定义: RCU is a sync ...

  4. java synchronized 线程同步机制详解

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...

  5. 深入理解synchronized方法同步的是方法还是对象?

    一.运用synchronized关键字 首先我们来看看一个多线程中线程不安全的列子 代码如下: 共享数据类: public class NotSynchronizated extends Thread ...

  6. Java的synchronized的同步代码块和同步方法的区别

    synchronized同步方法和同步代码块的区别 同步方法默认使用this或者当前类做为锁. 同步代码块可以选择以什么来加锁,比同步方法更精确,我们可以选择只有会在同步发生同步问题的代码加锁,而并不 ...

  7. 使用Synchronized块同步变量

    我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特定的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...

  8. 使用Synchronized关键字同步类方法

    要想解决“脏数据”的问题,最简单的方法就是使用synchronized关键字来使run方法同步,代码如下: public synchronized void run() { } 从上面的代码可以看出, ...

  9. Java多线程简析——Synchronized(同步锁)、Lock以及线程池

    Java多线程 Java中,可运行的程序都是有一个或多个进程组成.进程则是由多个线程组成的.最简单的一个进程,会包括mian线程以及GC线程. 线程的状态 线程状态由以下一张网上图片来说明: 在图中, ...

随机推荐

  1. 正则替换replace中$1的用法

    一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 1 2 3 4 5 stringObject.replace(regexp/substr,repla ...

  2. angular.js,IE7,8,9兼容性的处理

    转........... 这段时间详细了解了谷歌新出的MVVM框架angular.js,并直接在本人所从事的项目中使用了.但是使用新东西都是有风险的,这不,采用了新框架的页面IE7,8各种显示不出来… ...

  3. ab测试工具的使用

    下载地址:http://httpd.apache.org/download.cgi#apache24 编译安装后在安装目录bin下可以找到ab执行程序 基本用法: ab -n 5000 -c 1000 ...

  4. centos 7 私有云盘 OwnCloud 安装搭建脚本

    #!/bin/bash #Build LAMP Server Conf mysql_secure_installation service mariadb restart systemctl enab ...

  5. 11条sql技巧

    1. 负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好习惯 可以优化为in查询: ...

  6. tensorflow 源码编译tensorflow 1.1.0到 tensorflow 2.0,ver:1.1.0rc1、1.4.0rc1、1.14.0-rc1、2.0.0b1

    目录 tensorflow-build table 更多详细过程信息及下载: tensorflow-build tensorflow 源码编译,提升硬件加速,支持cpu加速指令,suport SSE4 ...

  7. 【POJ2376】Cleaning Shifts

    题目大意:区间最小覆盖问题. 题解:本身是一道贪心水题,但是细节还是比较多的,记录一下. 由于每个奶牛对答案的贡献是一样的,肯定要选择在满足条件的基础上能够拓展最多的那个奶牛.为了满足条件,对区间左端 ...

  8. Spring使用@AspectJ开发AOP(零配置文件)

    前言: AOP并不是Spring框架特有的.Spring只是支持AOP编程 (面向切面编程) 的框架之一. 概念: 1.切面(Aspect) 一系列Advice + Pointcut 的集合. 2.通 ...

  9. thinkPHP5.0.22初体验---request相关用法

    如果浏览器要返回美观排列的json数据,可以安装火狐浏览器的插件 返回XML的数据格式 渲染模板的用法 return $this->fetch('index/index2')效果 扒掉stirp ...

  10. C# JSON的序列化与反序列化

    需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然后使用Using: using System.Runtime.Serial ...