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. 25个免费的jQuery/ JavaScript的图表和图形库

    1.  JS Charts Features Prepare your chart data in XML, JSON or JavaScript Array Create charts in dif ...

  2. exits 和no exits

    exists : 强调的是是否返回结果集,不要求知道返回什么, 比如:  select name from student where sex = 'm' and mark exists(select ...

  3. JS常用自定义函数总结

    JS常用自定义函数总结   1.原生JavaScript实现字符串长度截取 2.原生JavaScript获取域名主机 3.原生JavaScript清除空格 4.原生JavaScript替换全部 5.原 ...

  4. Springmvc后台接前台数组,集合,复杂对象

    本人转载自: http://blog.csdn.net/feicongcong/article/details/54705933 return "redirect:/icProject/in ...

  5. electron builder 打包多个第三方依赖的软件

    背景 在实际的开发过程中,我们最后打包生成的exe.会依赖一些第三方的软件,或者说是一些系统的环境,比如 .net framework vc++ 等,这些环境不能依赖客户的环境,所以最好的做法是在打包 ...

  6. Hive分区表创建、分类

    一.分区表创建与说明 必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned ...

  7. Linux--Shell 编程-bash,命令替换,if分支嵌套,运算,输入输出

    SHELL 编程     shell 是一个命令解释器,侦听用户指令.启动这些指令.将结果返回给用户(交互式的shell)     shell 也是一种简单的程序设计语言.利用它可以编写一些系统脚本. ...

  8. Let's write a framework.

    Let's write a framework. create a model var model={a:1,b:'b'} let's create a router, router maps url ...

  9. 【hdu 6155】Subsequence Count

    题意 给定一个 \(01\) 串 \(S_{1...n}\) 和 \(Q\) 个操作. 操作有 \(2\) 种类型: 1. 将 \([l,r]\) 区间所有数取反(\(0→1,\space 1→0\) ...

  10. PHP的函数获取图片的宽高等信息

    PHP的函数getimagesize可以得到图片的宽高等信息 array getimagesize ( string $filename [, array &$imageinfo ] )   ...