class HL7ServerAdvanced{
public String receivemsgstring;
//获取内部类,SafeStorage是一个接口
public SafeStorage getSafeStorage() {
return new MySafeStorge();
}
class MySafeStorge implements SafeStorage{
//内部类中想要同步的函数
public void store(Transportable theMessage) throws FileNotFoundException,IOException{
receivemsgstring = "abc";
FileOutputStream fos = new FileOutputStream("recv.txt", true);
fos.write(receivemsgstring.getBytes());
fos.flush();
if (fos != null) {
fos.close();
fos = null;
}
}
public static void main(String[] args) {
final HL7ServerAdvanced hsd = new HL7ServerAdvanced();
class PrintSubThread extends Thread {
public void run() {
try {
while (true) {
sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值
if (hsd.receivemsgstring != null ) {
System.out.println(hsd.receivemsgstring);
hsd.receivemsgstring = null;
}
}
}
catch (InterruptedException iex) {
iex.printStackTrace();
}
}
}
class StoreSubThread extends Thread{
public void run(){
//调用内部类中的方法
hsd.getSafeStorage().store();
}
}
PrintSubThread pst = new PrintSubThread();
StoreSubThread sst = new StoreSubThread();
pst.start();
sst.start();
}
}我想将两个线程同步访问类中的变量receivemsgstring,StoreSubThread线程先对receivemsgstring调用内部类的store方法赋值并存储,再由PrintSubThread输出并置为空值。 我不知道如何加synchronized关键字来同步类中的变量receivemsgstring,还涉及了内部类的函数与类外的函数同时要操作类变量的问题,向大家指教,谢谢大家!
public String receivemsgstring;
//获取内部类,SafeStorage是一个接口
public SafeStorage getSafeStorage() {
return new MySafeStorge();
}
class MySafeStorge implements SafeStorage{
//内部类中想要同步的函数
public void store(Transportable theMessage) throws FileNotFoundException,IOException{
receivemsgstring = "abc";
FileOutputStream fos = new FileOutputStream("recv.txt", true);
fos.write(receivemsgstring.getBytes());
fos.flush();
if (fos != null) {
fos.close();
fos = null;
}
}
public static void main(String[] args) {
final HL7ServerAdvanced hsd = new HL7ServerAdvanced();
class PrintSubThread extends Thread {
public void run() {
try {
while (true) {
sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值
if (hsd.receivemsgstring != null ) {
System.out.println(hsd.receivemsgstring);
hsd.receivemsgstring = null;
}
}
}
catch (InterruptedException iex) {
iex.printStackTrace();
}
}
}
class StoreSubThread extends Thread{
public void run(){
//调用内部类中的方法
hsd.getSafeStorage().store();
}
}
PrintSubThread pst = new PrintSubThread();
StoreSubThread sst = new StoreSubThread();
pst.start();
sst.start();
}
}我想将两个线程同步访问类中的变量receivemsgstring,StoreSubThread线程先对receivemsgstring调用内部类的store方法赋值并存储,再由PrintSubThread输出并置为空值。 我不知道如何加synchronized关键字来同步类中的变量receivemsgstring,还涉及了内部类的函数与类外的函数同时要操作类变量的问题,向大家指教,谢谢大家!
我也不知道是否是这种情况...
如何对类中的变量receivemsgstring同步访问?
多谢大家指教!谢谢!
如果你只是想同步 receivemsgstring, 应该用 AtomicReference<V>, 不需要用 synchronize
public Object lock = new Object(); // 这个就是锁。在你的任何需要同步访问receivemsgstring的代码段,用synchronized(lock){
...
}
就可以了。
//在类中的函数
public void store(Transportable theMessage){
synchronized(lock){
...
}
}
//在要访问receivemsgstring的函数在类外
class HL7Application{
class HL7ServerAdvanced hsd = new HL7ServerAdvanced();
class PrintSubThread extends Thread {
public void run() {
while (true) {
sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值
synchronized(hsd.lock){
if (hsd.receivemsgstring != null ) {
System.out.println(hsd.receivemsgstring);
hsd.receivemsgstring = null;
}
}
}
}
}
}我想问一下lock和hsd.lock可否完成同步?是不是按上面的写法就正确了?谢谢!
class PrintSubThread extends Thread {
public void run() {
while (true) {
//sleep(500); //想让StoreSubThread线程先执行,目的是对receivemsgstring赋值
synchronized(hsd.lock){
hsd.lock.wait();
if (hsd.receivemsgstring != null ) {
System.out.println(hsd.receivemsgstring);
hsd.receivemsgstring = null;
}
}
}
}
}public void store(Transportable theMessage) {
synchronized(lock){
//函数中的其他内容.........
lock.notifyAll();
}
}这样很好的实现了线程的同步访问receivemsgstring ,谢谢java2000_net, 谢谢大家!