我对java的多线程不是很熟悉,找了很久也没找到合适的例子.我想实现这么个功能,在List里装有待处理的对象,用多个线程从List里取得对象并处理,把读取过的对象从List里删去..在操作list的时候加锁...我自己试着写了个可是不象并行运行的,有谁能给我个例子么?不胜感激...

解决方案 »

  1.   

    加锁只是在读list的时候加啊,读list占的时间很少吧..主要是为了并行花费时间长的处理.我自己瞎写的一个,完全不能用...能告诉我下是什么地方出问题了么?我现在在请教了别人以后会通过给list操作加synchronized来实现了..但是还是想知道我这个程序是什么问题.package threadtest;import java.util.ArrayList;
    import java.util.List;public class Threadtestold implements Runnable{
    private List<String> sourceList;
    private List<String> targetList=new ArrayList();
    private boolean isReading;
    private boolean isWriting;
    private String atom;

    public Threadtestold(int n,List list,boolean is){
    this.sourceList=list;
    this.isReading=is;
    }

    public synchronized void read(){
    if(isReading){
    try{
    wait();
    }catch(InterruptedException e){
    }
    }else{
    String ThreadName=Thread.currentThread().getName();
    isReading=true;
    long timeStart = System.currentTimeMillis();//test
    long time=0;
    if(sourceList.size()!=0){
    atom=sourceList.get(0);
    sourceList.remove(0);
    isReading=false;
    } notify();
    while(time>-1){
    long timeEnd = System.currentTimeMillis();//test
    time=(timeStart-timeEnd)/1000;
    }
    System.out.println(ThreadName+" is reading"+"the string is: "+atom); }
    }


    public void run(){
    while(sourceList.size()!=0){
    read();
    }

    }
    }
      

  2.   

      }else{
                String ThreadName=Thread.currentThread().getName();
                isReading=true;
                long timeStart = System.currentTimeMillis();//test
                long time=0;
                if(sourceList.size()!=0){
                    atom=sourceList.get(0);
                    sourceList.remove(0);
                    isReading=false;
                }            notify();
                while(time>-1){
                    long timeEnd = System.currentTimeMillis();//test
                    time=(timeStart-timeEnd)/1000;
      

  3.   

    代码贴错了吧?怎么run()方法后面完全不懂了
      

  4.   

    public synchronized void read()这说明同时只能有一个线程在执行这个函数
      

  5.   

    public synchronized void read()这说明同时只能有一个线程在执行这个函数 
    你还在函数利用了wait?????
      

  6.   

    你干脆用vector不就得了,直接不用考虑多线程得问题
      

  7.   

    确实不懂你的意图....好像用不着wait(),notify()吧
    再说构造每个Threadtestold对象的参数List是不是同一个啊
      

  8.   

    List 有一个 remove 方法! 你对这个方法加锁就可以了!像这样    public synchronized Object getItem(java.util.List list) {
    Object obj = null;
    if (list.size() <= 0)
    return obj;
    else
    return list.remove(0);
    }      
      

  9.   


    public class Factory {
        private static Factory factory;
        private static Object classLock = Factory.class;    private Factory() {}
        public static Factory getFactory() {
            synchronized (classLock) {
                if ( factory == null )
                    factory = new Factory();            return factory;
            }
        }
    }LZ说的应该是同步的问题
    以上代码,防止多个线程同时调用getFactory()方法
    仅供参考
      

  10.   

    我大概搞明白了.但是还有些问题..多线程的时候是比如我的类叫public class ThreadTest extends Thread{}
    那么每开一个线程是都要用Thread t=new ThreadTest()么,每个线程都是一个该类的实例?
    那比如我要多线程处理一些数据,然后最后我把这数据取出来是只能用引用把这数据传进去么?能不能做个get方法一类的?怎么能知道ThreadTest这个类生成的所有线程是不是都结束了?