import java.util.*;
public class Alighment {
public static void main(String args[]) {
int[] Alig = new int[10];
Ad a = new Ad(Alig);
De b = new De(Alig);
new Thread(a).start();
new Thread(b).start();
}
}class Alig{
int a[]=new int[10];
Alig(int a[]){
this.a = a;
}
public synchronized void ad(){
while(a[0]==0){
try{
Thread.sleep(1000);
}catch(Exception e){
//异常处理
}
for(int i=0;i<9;i++){
a[i]=a[i+1];
}
a[9]=(int)(Math.random()*100)+1;
System.out.println("添加了一个数:"+a[9]);
for(int i=0;i<10;i++)
System.out.println("输出:"+a[i]);
}

}
public synchronized void de(){
while(true){
try{
Thread.sleep(1000);
}catch(Exception e){
//异常处理
}
for(int i=0;i<10;i--){
System.out.println("删除了一个数:"+a[i]);
a[i]=0;
}
}
}
}class Ad implements Runnable{
int a[];
Ad(int a[]){
this.a=a;

}
public void run(){
new Alig(a).ad();
}
}
class De implements Runnable{
int a[];
De(int a[]){
this.a=a;
}
public void run(){
new Alig(a).de();
}
}
这个问题搞了两天了`
从队列后面添加的操作搞出来了`
但是从前面删除的操作这个线程怎么也不执行`
将删除的运行条件设置宽松点`
有的字段就直接报错`请高手指教`
如何才能让添加` 删除这两个线程同步运行`
请帮我改改删除线程的代码`

解决方案 »

  1.   

    这个同步写的8对。
    new Alig(a).ad();
    这个地方是新建了对象,而ad方法锁又是在对象上,两个线程,建了两个Alig对象,所以相当于没有锁。
    同时sleep方法是不会释放锁的。如果你锁在一个对象上,就死锁了。还有同步是用wait()和notify()做的,互斥才是用synchronized。你可以先找本书,看一下生产者和消费者的那个示例,应该会有些启发。
      

  2.   


    import java.util.*;
    public class Thead_test {
        public static void main(String args[]) {
            int[] Alig = new int[10];
            Ad a = new Ad(Alig);
            De b = new De(Alig);
            new Thread(a).start();
            new Thread(b).start();
        }
    }class Alig{
        int a[]=new int[10];
        Alig(int a[]){
            this.a = a;
        }
        public synchronized void ad(){
            while(a[0]==0){
                notify();
                for(int i=0;i<9;i++){
                    a[i]=a[i+1];        
                }
                a[9]=(int)(Math.random()*100)+1;
                System.out.println("添加了一个数:"+a[9]);            
                for(int i=0;i<10;i++)
                System.out.println("输出:"+a[i]);
                try{
                      wait();
                }catch (Exception e) {
    e.printStackTrace();
    }
            }
            
        }
        public synchronized void de(){
            while(true){
             notify();
                for(int i=9;i>=0;i--){
                        System.out.println("删除了一个数:"+a[i]);
                        a[i]=0;
                }
                try{
                    wait();
              }catch (Exception e) {
    e.printStackTrace();
    }
             
            }
        }
    }
    class Ad implements Runnable{
        int a[];
        Ad(int a[]){
            this.a=a;
        }
        public void run(){
            new Alig(a).ad();
        }
    }
    class De implements Runnable{
        int a[];
        De(int a[]){
            this.a=a;
        }
        public void run(){
            new Alig(a).de();
        }
    }
    改好了,你没加同步当然会乱啦
      

  3.   

    但是从前面删除的操作这个线程怎么也不执行;你的删除操作的线程不执行是因为你个方法被增加操作那个线程用了,而且抢夺了锁,他用完后也没有把锁释放掉,就这样这个增加操作的线程执行完了,也把你的数据都输出了,但是那个删除操作的线程还在苦苦地等待着这把锁, 增加操作那个家伙很缺德,用完就乱扔了.所以删除操作的线程不知道干什么.
    所以你应该在大家的线程上面都加上 notify();随时准备唤醒所有在锁池中的线程,wait()就是把当前线程的锁释放掉并且把自己推入等待池.其他线程已经在锁池中了,一旦获得刚刚被前一个线程释放的锁就可以执行了,就这样大家轮着执行
      

  4.   

    经过改动后,这个更加合你胃口,你应该是想做生产者消费者问题吧?
    import java.util.*;
    public class Thead_test {
        public static void main(String args[]) {
            int[] Alig = new int[10];
            Alig xx=new Alig(Alig);
            Ad a = new Ad(Alig,xx);
            De b = new De(Alig,xx);
            new Thread(a).start();
            new Thread(b).start();
        }
    }class Alig{
        int a[]=new int[10];
        Object lock=new Object();
        Alig(int a[]){
            this.a = a;
        }
        public  void ad(){
        
            while(a[0]==0){
             synchronized (lock) {
                lock.notify();
                for(int i=0;i<9;i++){
                    a[i]=a[i+1];        
                }
                a[9]=(int)(Math.random()*100)+1;
                a[8]=(int)(Math.random()*100)+1;           
                for(int i=0;i<10;i++)
                System.out.println("输出:"+a[i]);
                try{
                 lock.wait();
                }catch (Exception e) {
    e.printStackTrace();
    }
              
             }
               
            
         }
            
        }
        public  void de(){
         System.out.println(a[9]);
        
            while(true){
             synchronized (lock) {
             lock.notify();
                for(int i=9;i>=0;i--){
                 if(a[i]!=0){
                        System.out.println("删除了一个数:"+a[i]);
                        a[i]=0;
                 }
                }
                try{
                 lock.wait();
              }catch (Exception e) {
    e.printStackTrace();
    }
            }
            }
        }
    }class Ad implements Runnable{
        int a[];
        Alig xx;
        Ad(int a[],Alig xx){
            this.a=a;
            this.xx=xx;
        }
        public void run(){
          xx.ad();
        }
    }
    class De implements Runnable{
        int a[];
        Alig xx;
        De(int a[],Alig xx){
            this.a=a;
            this.xx=xx;
        }
        public void run(){
            xx.de();
        }
    }
      

  5.   

    哎` 
    自已改后确实删除操作能动了
    但还是达不到理想的效果`我希望的是添加操作在队列没满的时候能不停的往里加数据`
    删除操作在队列没空的时候能不停的减数据`
    而现在做的效果是队列先加完10个数字` 然后再删除10个数字`或者队列加一个` 然后马上就减掉一个`  跟单线程看起来差不多了`看来还是没理解synchronized的真谛`