list是ArrayList类的对象
F类要每隔几秒就往里写东西,
A类要每隔几秒都读取list,并会删除里面的数据
G类要每隔一秒都读取list,并显示出来。
F类和A类分别起一个线程,同时执行,对list操作请问要怎么设计?list要放在哪里?其实我在做操作系统的实验,磁盘移臂调度算法。F类模拟动态生成对磁盘的访问请求,A类模拟调度算法选择一个请求执行。
G类就是GUI了,要动态显示请求的生成与相应。list里面装的就是多个请求。

解决方案 »

  1.   

    放在应用程序,main里建立
    几个thread里有他一个引用,用来加锁
      

  2.   

    2楼说的不懂,我没写过线程,请说的详细点啊
    还有,A类和F类里面有一个While循环,对list不断调用,要是在main里建立list,怎么引用?啊!!!!!!!!
      

  3.   

    利用java.util.concurrent.LinkedBlockingQueue来做。建立一个单例的类,把LinkedBlockingQueue作为内部属性,并且提供get和set相应方法。这样无论任何一个类在任何地方都可以得到这个类的LinkedBlockingQueue对象,并操作其内部的数据。
      

  4.   

    加锁有一个专门的类,lock方法
    或者这样吧,用单态模式进行也应该是可以的吧
      

  5.   

    list是一个共享资源,有多个线程会操作这个资源,所以在list上的添加和取出操作需要同步。
      

  6.   

    public class ThreadA implements Runnable{
    private List l;
    public ThreadA(List l){
    this.l = l;
    }
       
        public void run(){
         while (true){
         try {
         Thread.sleep(2000);
         } catch (InterruptedException e) {
         e.printStackTrace();
         }
         synchronized(l){
         l.add(new String("Something"));
         }
         }
        }  
    }
    public class ThreadF implements Runnable{
    private List l;
    public ThreadF(List l){
    this.l = l;
    }
       
        public void run(){
         while (true){
         try {
         Thread.sleep(4000);
         } catch (InterruptedException e) {
         e.printStackTrace();
         }
         synchronized(l){
         if (!l.isEmpty()){
         Object o = l.remove(0);
         }
         }
         }
        }
    }
    public class ThreadG implements Runnable { private List l;
    public ThreadG(List l){
    this.l = l;
    }
       
        public void run(){
         while (true){
         try {
         Thread.sleep(1000);
         } catch (InterruptedException e) {
         e.printStackTrace();
         }
         synchronized(l){
         for(Object o:l){
         System.out.println(o);
         }
         System.out.println("Length:"+l.size());
         }
         }
        }
        
        public static void main(String[] args) throws InterruptedException{
         List l = new ArrayList();
         Thread threadA = new Thread(new ThreadA(l));
         Thread threadF = new Thread(new ThreadF(l));
         Thread threadG = new Thread(new ThreadG(l));
         threadA.setDaemon(true);
         threadF.setDaemon(true);
         threadG.setDaemon(true);
         threadA.start();
         threadF.start();
         threadG.start();
         Thread.sleep(60000);   
        }
    }
      

  7.   

    list是资源。
    生产者线程、消费者线程