package com.albert.thread;
import java.util.LinkedList;
import java.util.Random;
class Data
{
//保存时的文件名
private final String fileName;
//数据的内容
private String content;
//修改后的内容还没保存,为true
private boolean changed;
public Data(String fileName,String content)
{
this.fileName = fileName;
this.content = content;
this.changed = true;
}
//修改数据内容
public synchronized void change(String content)
{
this.content = content;
this.changed = true;
}
//若有数据修改 ,就保存在文件里
public synchronized void save()
{
if(!this.changed)
{
return;
}
doSave();
this.changed = false;
}
private void doSave() {
System.out.println(Thread.currentThread().getName()+" calls doSave content="+content);
System.out.println("内容写入文件:"+this.fileName);
}
}
class SaverThread extends Thread
{
private Data data;
public SaverThread(String name,Data data)
{
super(name);
this.data = data;
}
public void run()
{
while(true)
{
//保存数据
data.save();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ChangerThread extends Thread
{
private Data data;
private Random random = new Random();
public ChangerThread(String name,Data data)
{
super(name);
this.data = data;
}
public void run()
{
for(int i=0;true;i++)
{
data.change("No."+i);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
//明确的要求存档
data.save();
}
}
}
public class MyThread {
private static final long CALL_COUNT=100000L;
/**
* @param args
*/
public static void main(String[] args) {
Data data = new Data("a.txt","(empty)");
new ChangerThread("ChangerThread",data).start();
new SaverThread("SaverThread",data).start();
}
}
import java.util.LinkedList;
import java.util.Random;
class Data
{
//保存时的文件名
private final String fileName;
//数据的内容
private String content;
//修改后的内容还没保存,为true
private boolean changed;
public Data(String fileName,String content)
{
this.fileName = fileName;
this.content = content;
this.changed = true;
}
//修改数据内容
public synchronized void change(String content)
{
this.content = content;
this.changed = true;
}
//若有数据修改 ,就保存在文件里
public synchronized void save()
{
if(!this.changed)
{
return;
}
doSave();
this.changed = false;
}
private void doSave() {
System.out.println(Thread.currentThread().getName()+" calls doSave content="+content);
System.out.println("内容写入文件:"+this.fileName);
}
}
class SaverThread extends Thread
{
private Data data;
public SaverThread(String name,Data data)
{
super(name);
this.data = data;
}
public void run()
{
while(true)
{
//保存数据
data.save();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ChangerThread extends Thread
{
private Data data;
private Random random = new Random();
public ChangerThread(String name,Data data)
{
super(name);
this.data = data;
}
public void run()
{
for(int i=0;true;i++)
{
data.change("No."+i);
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
//明确的要求存档
data.save();
}
}
}
public class MyThread {
private static final long CALL_COUNT=100000L;
/**
* @param args
*/
public static void main(String[] args) {
Data data = new Data("a.txt","(empty)");
new ChangerThread("ChangerThread",data).start();
new SaverThread("SaverThread",data).start();
}
}