/**
* 数据不一致问题
* stop
*/
public class StopDemo {
public static Student student = new Student();
public static class Student{
private int id;
private String name;
public Student() {
id = 0;
name = "0";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public static class ChangeObjectThread extends Thread{
@Override
public void run() {
while (true){
synchronized (student){
int v = (int) (System.currentTimeMillis()/1000);
student.setId(v);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
student.setName(String.valueOf(v));
}
Thread.yield();
}
}
}
public static class ReadObjectThread extends Thread{
@Override
public void run() {
while (true){
synchronized (student){
if (student.getId()!=Integer.parseInt(student.getName())){
System.out.println(student.toString());
}
}
Thread.yield();
}
}
}
public static void main(String[] args) throws InterruptedException{
new ReadObjectThread().start();
while (true){
Thread t = new ChangeObjectThread();
t.start();
Thread.sleep(150);
t.stop();
}
}
//Student{id=1566203521, name='1566203520'}
//Student{id=1566203521, name='1566203520'}
//Student{id=1566203521, name='1566203520'}
//Student{id=1566203521, name='1566203520'}
//Student{id=1566203521, name='1566203520'}
//Student{id=1566203522, name='1566203521'}
//Student{id=1566203522, name='1566203521'}
//Student{id=1566203522, name='1566203521'}
//Student{id=1566203522, name='1566203521'}
//Student{id=1566203522, name='1566203521'}
//Student{id=1566203522, name='1566203521'}
//Student{id=1566203523, name='1566203522'}
//Student{id=1566203524, name='1566203523'}
//Student{id=1566203524, name='1566203523'}
//.....
}
/**
* 正确stop方式
*/
public class StopDemo2 {
public static Student student = new Student();
public static class Student{
private int id;
private String name;
public Student() {
id = 0;
name = "0";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public static class ChangeObjectThread extends Thread{
volatile boolean stopme = false;
public void stopMe(){
stopme = true;
}
@Override
public void run() {
while (true){
if (stopme){
System.out.println("exit by stop me");
break;
}
synchronized (student){
int v = (int) (System.currentTimeMillis()/1000);
student.setId(v);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
student.setName(String.valueOf(v));
}
Thread.yield();
}
}
}
public static class ReadObjectThread extends Thread{
@Override
public void run() {
while (true){
synchronized (student){
if (student.getId()!=Integer.parseInt(student.getName())){
System.out.println(student.toString());
}
}
Thread.yield();
}
}
}
public static void main(String[] args) throws InterruptedException{
new ReadObjectThread().start();
while (true){
Thread t = new ChangeObjectThread();
t.start();
Thread.sleep(150);
((ChangeObjectThread) t).stopMe();
}
}
}