互联网并发编程之一 线程安全03线程中的脏读
2017-12-13 00:31
561 查看
当一个线程运行修改数据的方法,另一个线程启动读取数据的值,会读到未修改的前的数据
package com.jc.thread;
public class Thread04 {
private String name = "abc";
private String password = "123";
public synchronized void setValue(String name, String password) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.password = password;
}
public void getValue() {
System.out.println(this.name);
System.out.println(this.password);
}
public static void main(String[] args) {
final Thread04 thread04 = new Thread04();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
thread04.setValue("jkl", "333");
}
}, "t1");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread04.getValue();
}
}
输出结果为:
abc
123
对于对象的同步或者异步方法,我们在设计程序的时候一定要考虑整体性,不然会出现不一致的问题,很经典的错误就是脏读,当setValue()和getValue()同时加上synchronized 关键字,保证了业务的原子性。
package com.jc.thread;
public class Thread04 {
private String name = "abc";
private String password = "123";
public synchronized void setValue(String name, String password) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.password = password;
}
public synchronized void getValue() {
System.out.println(this.name);
System.out.println(this.password);
}
public static void main(String[] args) {
final Thread04 thread04 = new Thread04();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
thread04.setValue("jkl", "333");
}
}, "t1");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread04.getValue();
}
}
输出结果为:
jkl
333
package com.jc.thread;
public class Thread04 {
private String name = "abc";
private String password = "123";
public synchronized void setValue(String name, String password) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.password = password;
}
public void getValue() {
System.out.println(this.name);
System.out.println(this.password);
}
public static void main(String[] args) {
final Thread04 thread04 = new Thread04();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
thread04.setValue("jkl", "333");
}
}, "t1");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread04.getValue();
}
}
输出结果为:
abc
123
对于对象的同步或者异步方法,我们在设计程序的时候一定要考虑整体性,不然会出现不一致的问题,很经典的错误就是脏读,当setValue()和getValue()同时加上synchronized 关键字,保证了业务的原子性。
package com.jc.thread;
public class Thread04 {
private String name = "abc";
private String password = "123";
public synchronized void setValue(String name, String password) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.password = password;
}
public synchronized void getValue() {
System.out.println(this.name);
System.out.println(this.password);
}
public static void main(String[] args) {
final Thread04 thread04 = new Thread04();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
thread04.setValue("jkl", "333");
}
}, "t1");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread04.getValue();
}
}
输出结果为:
jkl
333
相关文章推荐
- 互联网并发编程(1)--线程基础
- Java并发编程规则:有状态的线程安全对象在线程池中使用不一定就是线程安全的
- Java并发编程-03-守护线程的创建和运行
- 互联网并发编程(2)--线程基础
- [C++11 并发编程] 03 - 向线程传递参数
- 互联网并发编程之一 线程安全02
- 【Java并发编程】之十二:线程间通信中notifyAll造成的早期通知问题(含代码)
- (十二)java并发编程--线程优先级
- Java并发编程规则:设计线程安全的类
- Java 编程下的并发线程之间的同步代码块死锁
- java线程学习(九)—并发编程实践学习
- 互联网并发编程(3)--脏读
- PAIP.并发编程 多核编程 线程池 ExecutorService的判断线程结束
- Java并发编程】之四:守护线程与线程阻塞的四种情况
- 多线程编程15-----Semaphore类限制并发线程数
- 转:【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- Java并发编程:进程和线程之由来
- Java 并发编程:线程间的协作(wait/notify/sleep/yield/join)
- JAVA 并发编程随笔【五】Thread线程创建及运行线程任务
- JAVA 并发编程随笔【五】Thread线程创建及运行线程任务