首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java线程间同步的有关问题

2012-01-19 
Java线程间同步的问题大家好:我的程序中定义了ThreadClass1 ThreadClass2 两个类(从Thread继承)。有一个全

Java线程间同步的问题
大家好:
  我的程序中定义了ThreadClass1 ThreadClass2 两个类(从Thread继承)。
  有一个全局的数组A[10]
  ThreadClass1中会write A[]
  ThreadClass2中会Read A[] 
  为了保证A[0]..A[9]是ThreadClass1在同一次写入的数据,
  该如何写?能提供例程吗? 

  谢谢!
 

[解决办法]
参考 notify() 和wait() 方法的用法 好象有个经典的生产者消费者的例子应该是处理这类问题的.
给一段参考代码吧:

Java code
public class WaitComm {    public static void main(String[] args) {        WFlagSend s = new WFlagSend();        WFlagRec r = new WFlagRec(s);        Thread st = new Thread(s);        Thread rt = new Thread(r);        rt.setDaemon(true);        st.start();        rt.start();        try {            st.join();            while ( s.isValid ){                Thread.sleep(100);            }        }catch(InterruptedException e){            e.printStackTrace();            }    }}class WFlagSend implements Runnable {     int theValue;     boolean isValid;        public void run() {        for ( int i=0; i<5; i++){            synchronized(this){                while (isValid){                    try{                        this.wait();                    }catch(Exception e){e.printStackTrace();}                }            }            theValue = (int)(Math.random()*256);            System.out.println("sending " + theValue );            synchronized(this){                isValid = true;                this.notify();            }                    }    }}class WFlagRec implements Runnable {    private WFlagSend theSender;    public WFlagRec(WFlagSend sender){        theSender = sender;        }    public void run() {        while ( true ) {            synchronized(theSender) {                while ( !theSender.isValid ){                    try{                        theSender.wait();                    }catch(Exception e){e.printStackTrace();}                }                        }            System.out.println("received " + theSender.theValue);            synchronized(theSender) {                theSender.isValid = false;                    theSender.notify();            }        }        }}
[解决办法]
Java code
class Test{       public static void main(String[] args)       {               final Arrays a = new Arrays();               Thread t1 = new Thread()               {                       public void run()                       {                               int j=1;                               while(true)                               {                                       a.write(j++);                               }                       }               };               Thread t2 = new Thread()               {                       public void run()                       {                               while(true)                               {                                       a.read();                               }                       }               };               t1.start();               t2.start();       }}class Array{       int n = 0;       int[] num = new int[n];       boolean b = false;       synchronized void write(int j)       {               int n = 0;               try               {                       if (b)                       {                               wait();                       }               }               catch(Exception e)               {                       System.out.println(e);               }               num[n++] = j;               System.out.println(Thread.currentThread().getName()+" "+num[n]);               b = true;               notify();       }       synchronized void read()       {               try               {                       if(!b)                       {                       wait();                       }               }               catch(Exception e)               {                       System.out.println(e);               }               System.out.println(Thread.currentThread().getName()+" "+num[n]);               b = false;               notify();       }} 


[解决办法]

Java code
 
public class Synchronized {
static class Account{
private double money=1000.0d;
public void nonSynDeposit(double fFees){
System.out.println("Account nonSynDeposit begin! money= "+this.money+"fFees= "+fFees);
System.out.println("Account nonSynDeposit sleep begin!");
try{
Thread.sleep(300);

}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Account nonSynDeposit end! money= "+this.money);
}

public void nonSynWithdraw(double fFees){
System.out.println("Account nonSynWithdraw begin! money= "+this.money+" fFees= "+fFees);
System.out.println("Account nonSynWithdraw sleep begin!");
try{
Thread.sleep(400);

}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Account nonSynWithdraw sleep begin!");
this.money=this.money-fFees;
System.out.println("Account nonSynWithdraw end money= "+this.money+" fFees= "+fFees);
}

public synchronized void synDeposit(double fFees){
System.out.println("Account synDeposit begin! money= "+this.money+" fFees= "+fFees);
System.out.println("Account synDeposit sleep begin!");
try{
Thread.sleep(300);

}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Account synDeposit begin!");
this.money=this.money+fFees;
System.out.println("Account synDepost end! money= "+this.money+" fFees= "+fFees);
}

public synchronized void synWithdraw(double fFees){
System.out.println("Account synWithdraw begin! money= "+this.money+" fFees= "+fFees);
System.out.println("Account synWithdraw sleep begin!");
try{
Thread.sleep(400);

}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Account synWithdraw sleep end!");
this.money=this.money-fFees;
System.out.println("Account synWithdraw end! money= "+this.money+" fFees= "+fFees);
}
static class AccessThread extends Thread{
private Account account=null;
private String method="";

public AccessThread(Account account,String method){
this.method=method;
this.account=account;
}
public void run(){
if(method.equals("nonSynDeposit")){
account.nonSynDeposit(500.0);
}else if(method.equals("nonSynWithdraw")){
account.nonSynWithdraw(200.0);
}else if(method.equals("synDeposit")){
account.synDeposit(500.0);
}else if(method.equals("synWithdraw")){
account.synWithdraw(200.0);
}
}

}



public static void main(String []args){
Account account=new Account();
System.out.println("使用非同步方法时:");
Thread threadA=new AccessThread(account,"nonSynDeposit");
Thread threadB=new AccessThread(account,"nonSynWithdraw");
threadA.start();
threadB.start();
try{
threadA.join();
threadB.join();

}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("");


account=new Account();
System.out.println("使用同步方法时:");
threadA=new AccessThread(account,"SynDeposit");
threadB=new AccessThread(account,"SynWithdraw");
threadA.start();
threadB.start();
}

}

}

热点排行
Bad Request.